リスト (p100)
リスト処理(その1)
- (cons 要素 リスト
- すでに存在するリストの先頭に要素を追加した新しいリストを返す
- 存在しない場合は、新たにリストを作る
- (list 要素を並べる)
- 引数を要素とするリストを作成する
- (list nil)とすると「nil」という要素1つのリストができる
- (list)とすると要素ゼロのリストができる。既存のリストのクリアに使える
- (car リスト)
- リストの先頭の要素を取り出す。引数のリストそのものは変化しない
- (cdr リスト)
- リストの2番目以降の要素からなるリストを返す。引数のリストのそのものは変化しない
- append
- 引数として与えられた任意個のすべてのリスト群からなるリストを新たに作る。リストどうしの結合に用いる
- (equal オブジェクト1 オブジェクト2)
- オブジェクト1,2の内容を比較し等価であればt、それ以外はnilを返す
- eq
- equalと同様に比較するが完全に同一オブジェクトの場合のみtを返す
リスト処理(その2)
リストの作り方、要素の数がどうなるかの検証
;;;
(defun my-list-test-0 ()
"listのテスト: listを使ってリストを作ったときの要素の数"
(interactive)
(let (aa bb cc dd ee)
(setq aa (list "a" "b" "c"))
(message "length aa=%d" (length aa))
(sit-for 3))) ; =======> 3になる
(defun my-list-test-1 ()
"listのテスト: (list nil)のときの要素の数"
(interactive)
(let (aa bb cc dd ee)
(setq aa (list "a" "b" "c"))
(setq aa (list nil)) ; nilはnilという値がリストに入るのでクリアしたことにはならない
(message "length aa=%d" (length aa))
(sit-for 3))) ; =======> 1になる つまり(list nil)で上書きしたことになる
(defun my-list-test-2 ()
"listのテスト: (list)のときの要素の数"
(interactive)
(let (aa bb cc dd ee)
(setq aa (list "a" "b" "c"))
(setq aa (list)) ; リストのクリアに使用できる
(message "length aa=%d" (length aa))
(sit-for 3))) ; =======> 0になる
(defun my-list-test-3 ()
"listのテスト: (list)のときはnil?"
(interactive)
(let (aa bb cc dd ee)
(setq aa (list "a" "b" "c"))
(setq aa (list))
(if (eq nil aa)
(message "aa=nil") ; =========> nilになる
(message "aaはnilではない"))))
(defun my-list-test-4 ()
"listのテスト: consを使った場合の要素の数"
(interactive)
(let (aa bb cc)
(setq aa (cons 111 aa))
(message "length aa=%d" (length aa)))) ; =====> 1になる
(defun my-list-test-5 ()
"listのテスト: consを2回使った場合の要素の数"
(interactive)
(let (aa bb cc)
(setq aa (cons 111 aa))
(setq aa (cons 222 aa))
(message "length aa=%d" (length aa)))) ; =====> 2になる
(defun my-list-test-6 ()
"listのテスト: 最初にlistでリストを作り、その後consを使った場合の要素の数"
(interactive)
(let (aa bb cc)
(setq aa (list "111" "222" "333"))
(setq aa (cons "444" aa))
(message "length aa=%d" (length aa)))) ; =====> 4になる
split-string, nth, length
文字列の分割、リストの要素の数を知る、リストの要素を番号指定で取り出す
(defun test-nth ()
"splitとnthのテスト"
(interactive)
(let (aa list len yy0 yy3 ppp)
(setq aa "1st 2nd 3rd 4th")
(setq list (split-string aa " "))
(setq len (length list))
(setq yy0 (nth 0 list))
(setq yy3 (nth 3 list))
(message "len=%d, yy0=%s, yy3=%s" len yy0 yy3)
(sit-for 20)))
(setq book (nth 0 ppp))
出力は ==> len=4, yy0=1st, yy3=4th