chapter7 基本函数:car, cdr, cons
car:Contents of the Address part of the Register(寄存器地址部分的内容)
cdr:Contents of the Decrement part of the Register(寄存器后部内容)
cons:Construct(构造)
7.1 car和cdr函数
一个列表的car,就是返回这个列表的第一个元素:
(car '(rose violet daisy buttercup))
car仅仅报告列表的第一个元素是什么,列表依旧是自己。
一个列表的cdr就是这个列表的其余部分(除第一个元素外的其余部分)
(cdr '(rose violet daisy buttercup))
7.2 cons函数
cons函数可以构造列表
(cons 'pine '(fir oak maple))
cons函数将一个新元素放在一个列表的开始处。
cons必须有一个待插入元素的列表。
(cons 'buttercup ()) => (buttercup)
查询列表的长度:length函数
(length '(daisy buttercup)) =>2
(length ()) => 0
(length ) => wrong
7.3 nthcdr函数
(cdr (cdr '(pine fir oak maple))) => (oak maple)
(nthcdr 2 '(pine fire oak maple)) => (oak maple)
nthcdr类似于重复调用cdr函数一样。
7.4 setcar函数
setcar和setcde函数将一个列表的car和cdr设置一个新的值。这里实际上该表了原始列表。
(setq animal '(giraffe antelope tiger lion))
animal
(setcar animal 'hippopotamus)
在列表中增加了一个元素。
7.5 setcdr函数
(setq domesticated-animals '(horse cow sheep goat))
(setcdr domesticated-animals '(cat dog))
domesticated-animals
转载于:https://blog.51cto.com/zmhot88/1206932