Upcoming SlideShare
×

# PAIP 第1章 Lisp入門

1,545 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
1,545
On SlideShare
0
From Embeds
0
Number of Embeds
388
Actions
Shares
0
5
0
Likes
0
Embeds 0
No embeds

No notes for slide

• ### PAIP 第1章 Lisp入門

1. 1. PAIP 1 Lisp yadokarielectric | yad-EL
2. 2. • 1.10 • 1 • • 1
3. 3. •1 1 • 1 1 • 1 1 • •
4. 4. • • OK •
5. 5. • • • Lisp
6. 6. • Lisp •( 1 2 …) • • (+ 1 (* 2 3 4) 5)
7. 7. • • • (if then else ) • if then elese
8. 8. • • setf • (setf abc 3)
9. 9. • • • • 2 2
10. 10. • (setf x (1 2 3)) (1 2 3) 1 • (setf x (list 1 2 3))
11. 11. quote • quote • (setf x (quote (1 2 3))) • ' • (setf x '(1 2 3))
12. 12. (setf p '(JOHN Q PUBLIC)) • (ﬁrst p) • (cons 'Mr p) • (rest p) • (cons (ﬁrst p) (rest p)) • (second p) • (last p) • (third p) • (ﬁrst (last p)) • (fourth p) • (length p)
13. 13. • defun • (defun ( 1 2…) " " …)
14. 14. • • mapcar • function #' • (mapcar #'last-name names)
15. 15. 1.1 (defparameter *suffix* '(MD Jr. |,|)) (defun last-name (name) (if (member (ﬁrst (last name)) *suffix*) (last-name (butlast name)) (ﬁrst (last name))))
16. 16. n=28 1.2 loop-power 27 power (defun loop-power (x n) (let ((r 1)) 28 2 (dotimes (i n) 14 2 (setf r (* r x))) 7 x r)) 6 2 3 x (defun square (x) (* x x)) 2 2 (defun power (x n) 6 (cond ((= n 0) 1) ((evenp n) (square (power x (/ n 2)))) (t (* x (power x (- n 1))))))
17. 17. 1.3 nil (defun count-atoms (exp) (cond ((null exp) 0) ((atom exp) 1) (t (+ (count-atoms (ﬁrst exp)) (count-atoms (rest exp))))))
18. 18. 1.3 nil (defun count-all-atoms (exp &optional (if-null 1)) (cond ((null exp) if-null) ((atom exp) 1) (t (+ (count-all-atoms (ﬁrst exp) 1) (count-all-atoms (rest exp) 0)))))
19. 19. 1.4 (defun count-anywhere (item tree) (cond ((eql item tree) 1) ((atom tree) 0) (t (+ (count-anywhere item (ﬁrst tree)) (count-anywhere item (rest tree))))))
20. 20. 1.5 (defun dot-product (a b) (if (or (null a) (null b)) 0 (+ (* (ﬁrst a) (ﬁrst b)) (dot-product (rest a) (rest b))))) (defun dot-product (a b) (let ((sum 0)) (dotimes (i (length a)) (incf sum (* (elt a i) (elt b i)))) sum)) (defun dot-product (a b) (apply #'+ (mapcar #'* a b)))