-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch5.lisp
70 lines (48 loc) · 1.69 KB
/
ch5.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;; optional parameters to a function/procedure definition are prepended by & optional
; [1]> (defun foo (a b &optional c d) (list a b c d))
; FOO
; [2]> (foo 1 2
; )
; (1 2 NIL NIL)
; [3]> (foo 1 2 3 4)
; (1 2 3 4)
; [4]>
;; default values can be set as we saw in ch3
; [4]> (defun foo (a b &optional (c 10) d) (list a b c d))
; FOO
; Break 1 [7]> (foo 1 2)
; (1 2 10 NIL)
;; supplied-p lets you understand if the optional parameter was passed or not
; Break 1 [7]> (defun foo (a b &optional (c 10 c-supplied-p) d) (list a b c d c-supplied-p))
; FOO
; Break 1 [7]> (foo 1 2 3)
; (1 2 3 NIL T)
; Break 1 [7]> (foo 1 2)
; (1 2 10 NIL NIL)
;; &rest is how you do N arg function calls
;; &key we learned about in ch3 - kwargs
;; like python def square(x=10, y=10) == (defun square(&key x y))
;; setting/getting the value of the parameter is how (select) and (where) worked using :param
;; of course, all preceding things work in this context as well
; (defun foo (x &optional y &key z) (list x y z))
;; breaking from do/for loops
(defun foo (n)
(dotimes (i 10)
(dotimes (j 10)
(when (> (* i j) n)
(return-from foo (list i j))))))
;; (funcall ) is a way to call a previously defined function
; (foo 1 2 3) === (funcall #'foo 1 2 3)
; (foo 1 2 3) === (funcall 'foo 1 2 3)
;; this is only useful when you're defining a procedure which takes a procedure as an argument, i.e
(defun plot (fn min max step)
(loop for i from min to max by step do
(loop repeat (funcall fn i) do (format t "*"))
(format t "~%")))
;; apply is weird
; (apply #'plot '(exp 2 3 4))
;; which is better?
; (defun double(x) (* 2 x))
; (plot #'double 0 10 1)
;; or
; (plot #'(lambda (x) (* 2x)) 0 10 1)