GNU emacs Lisp小結4

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章