引き続きLispを勉強中
公開日:
:
最終更新日:2014/01/29
LISP
記事内に広告を含む場合があります。記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
なんか面白くなってきた!!
(setq a '(1 2 3)) ; (1 2 3)
(setq b a) ; (1 2 3)
(eq a b) ; t
(setq b '(1 2 3)) ; (1 2 3)
(eq a b) ; nil
(equal a b) ; t
(integerp 3.4) ; nil
(floatp 3.4) ; t
(numberp "string") ; nil
(functionp 'car) ; t
(functionp car) ; (void-variable car)
(setq vec [1 2 3 4 5]) ; [1 2 3 4 5]
(sequencep vec) ; t
; sequence とは、リストと配列のこと
(arrayp vec) ; t
(arrayp '(a b c)) ; nil
(arrayp (a b c)) ; (void-function a)
(arrayp [a b c]) ; t
(vectorp vec) ; t
; 配列には、ベクタと文字列の2種類が存在する
(stringp vec) ; nil
(boundp 'x) ; nil
(fboundp 'car) ; t
(null nil) ; t
(zerop 20) ; nil
(wholenump -1) ; nil
; 評価されることを意図したS式は「フォーム」と呼ばれる
(defun add3 (x)
(+ x 3)) ; add3
(add3 4) ; 7
((lambda (x) (+ x 3)) 4) ; 7
((lambda (x y z) (format "%s %s %s" x y z)) 1 2 3) ; "1 2 3"
((lambda (x &optional y z) (format "%s %s %s" x y z)) 1 2) ; "1 2 nil"
((lambda (x &optional y &rest z) (format "%s %s %s" x y z)) 1 2 3 4 5
6 7) ; "1 2 (3 4 5 6 7)"
((lambda (x &optional y &rest z) (format "%s %s %s" x y z)) 1) ; "1 nil nil"
((lambda (x &optional y &rest z) (format "%s %s %s" x y z)) 1 2) ; "1 2 nil"
((lambda (x &optional y &rest z) (format "%s %s %s" x y z)) 1 2 3) ; "1 2 (3)"
((lambda (x &optional y &rest z) (format "%s %s %s" x y z)) 1 2 3 4) ;
"1 2 (3 4)"
(defun sample (nums)
(cond
((null nums) nil)
((atom nums) (when (>= num 3) nums))
((>= (car nums) 3) (car nums))
(t (sample (cdr nums))))
) ; sample
(sample '(0 1 2 4 8)) ; 4
(setq z 10) ; 10
((lambda (x y) (+ x y z)) 1 2) ; 13
(setq x 10) ; 10
((lambda (x y) (+ x y)) 2 5) ; 7
(setq x 10) ; 10
((lambda (y) (+ x y)) 5) ; 15
(defun f1 (x y) (f2 3 6)) ; f1
(defun f2 (a b) (list x)) ; f2
(f1 5 10) ; (5)
(setq dls (cons 'a 'b)) ; (a . b)
(car dls) ; a
(cdr dls) ; b
(cons 'a 'b) ; (a . b)
(list) ; nil
(list nil) ; (nil)
(list 1) ; (1)
(list 1 2) ; (1 2)
(cons 'a (cons 'b nil)) ; (a b)
(list 'a 'b) ; (a b)
(append) ; nil
(append nil) ; nil
(append nil nil) ; nil
(append nil nil nil) ; nil
(make-list 0 123) ; nil
(make-list 1 123) ; (123)
(make-list 2 123) ; (123 123)
(cons 'a '(b c)) ; (a b c)
(cons 'a nil) ; (a)
(cons 'a 'b) ; (a . b)
(cons '(a b) 'c) ; ((a b) . c)
(list 'a 'b) ; (a b)
(list 'a 'b 'c) ; (a b c)
(list '(a b) '(c d)) ; ((a b) (c d))
(append '(a b) '(c d)) ; (a b c d)
(append '(a b) (list 'c)) ; (a b c)
(append '(a b) 'c) ; (a b . c)
(make-list 4 'a) ; (a a a a)
(make-list 4 '(a b)) ; ((a b) (a b) (a b) (a b))
(nth 0 '(1 2 3)) ; 1
(nthcdr 2 '(1 2 3)) ; (3)
(setq editors '("Meadow" "Emacs" "vi")) ; ("Meadow" "Emacs" "vi")
(nth 1 editors) ; "Emacs"
(nthcdr 1 editors) ; ("Emacs" "vi")
(member "Emacs" editors) ; ("Emacs" "vi")
(member "Hidemaru" editors) ; nil
(remove "Emacs" editors) ; ("Meadow" "vi")
(reverse editors) ; ("vi" "Emacs" "Meadow")
(setq target '(1 4 5 7 2 0)) ; (1 4 5 7 2 0)
(sort (copy-sequence target) '<) ; (0 1 2 4 5 7)
target ; (1 4 5 7 2 0)
(setq result (sort target '<)) ; (0 1 2 4 5 7)
target ; (1 2 4 5 7)
result ; (0 1 2 4 5 7)
(setq color-alist '((red . apple) (blue . ocean) (green . leaf))) ;
((red . apple) (blue . ocean) (green . leaf))
(assoc 'red color-alist) ; (red . apple)
(rassoc 'leaf color-alist) ; (green . leaf)
(setq new-alist (copy-alist color-alist)) ; ((red . apple) (blue .
ocean) (green . leaf))
new-alist ; ((red . apple) (blue . ocean) (green . leaf))
(setq color-alist '(("red" . apple) ("blue" . ocean) ("green" .
leaf))) ; (("red" . apple) ("blue" . ocean) ("green" . leaf))
(assoc-default nil color-alist '(lambda (x y) (> (length x) 4))) ; leaf
(setq alist '((a . 1) b (c . 3) d)) ; ((a . 1) b (c . 3) d)
(assoc-default 'b alist nil t) ; t
(assoc-default 'c alist nil t) ; 3
(assoc-default 'e alist nil t) ; nil
(setq person '(blood ab)) ; (blood ab)
(plist-get person 'blood) ; ab
(plist-put person 'age 25) ; (blood ab age 25)
person ; (blood ab age 25)
(setplist 'car '(type "sedan" color "red")) ; (type "sedan" color "red")
(get 'car 'color) ; "red"
(put 'car 'color "black") ; "black"
(symbol-plist 'car) ; (type "sedan" color "black")
関連記事
- PREV
- CodeReposのcommit権をもらった!
- NEXT
- 「キャズム」を読んだ