リスト
リファレンスマニュアルへのリンク
リストを作る
;;list関数を使った方法
(list 'a 'b 'c 'd) ;=> (a b c d)
;;シングルクォートを使った方法
'(a b c d) ;=> (a b c d)
;;consを使った方法
(cons 'a '(b c d)) ;=> (a b c d)
(cons 'a (cons 'b '(c d))) ;=> (a b c d)
リストの最初の要素を取り出す
(car '(a b c d)) ;=> a
(first '(a b c d)) ;=> a
リストのn番目の要素を取り出す
;;nthは0からn-1の要素を取り出す関数
(nth 0 '(0 1 2 3 4 5)) ;=> 0
(nth 3 '(0 1 2 3 4 5)) ;=> 3
リストの最後の要素を取り出す
;;最後の要素をリストで取り出す
(last '(a b c d)) ;=> (d)
;;最後の要素をアトムとして取り出す
(defun last1 (lst)
(car (last lst)))
(last1 '(a b c d)) ;=> d
リストのn番目以降を取り出す
(nthcdr 0 '(0 1 2 3 4)) ;=> (0 1 2 3 4)
(nthcdr 3 '(0 1 2 3 4)) ;=> (3 4)
リストの要素数を取得する
(length '(1 2 3 4)) ;=> 4
リストが空かどうかを調べる
(null ()) ;=> t
(null '(a b c d)) ;=> nil
リストを結合する
;;consの場合
(cons 'a nil) ;=> (a)
(cons 'a '(b c)) ;=> (a b c)
(cons '(a b) '(c d)) ;=> ((a b) c d)
;;append
(append '(a b) '(c d)) ;=> (a b c d)
(append '((a b) (c d)) '(e f))
;=> ((a b) (c d) e f)
(append '((a b) (c d)) '((e f)))
;=> ((a b) (c d) (e f))