-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.lisp
141 lines (100 loc) · 2 KB
/
functions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(defun plus (a b)
(+ a b))
;; => PLUS
(plus 2 5)
;; => 7
(defun another-plus (a b)
(setf a 1337)
(+ a b))
;; => ANOTHER-PLUS
(another-plus 2 5)
;; => 1342
(defun plus (a b)
"Plus some numbers"
(+ a b))
;; => PLUS
(documentation 'plus 'function)
;; => "Plus some numbers"
(defun plus (a b c)
(+ a b c))
;; => PLUS
(plus 2 5 11)
;; => 18
(plus 2 5)
;; ERROR!!!
(defun plus (a b &optional c)
(if c (+ a b c)
(+ a b)))
;; => PLUS
(plus 3 2 1)
;; => 6
(plus 3 2)
;; => 5
(plus 3 2 nil)
;; => 5
(defun plus (a b &optional (c 0))
(+ a b c))
;; => PLUS
(plus 3 2)
;; => 5
(plus 3 2 1)
;; => 6
(defun say-hello (&optional (name "anon" name-supplied-p))
(format nil "Hello ~a (~a)" name name-supplied-p))
;; => SAY-HELLO
(say-hello)
;; => "Hello anon (NIL)"
(say-hello "DAVE")
;; => "Hello DAVE (T)"
(say-hello "anon")
;; => "Hello anon (T)"
(defun plus (&optional (a 0 a-p) (b 0 b-p))
(princ a-p)
(princ b-p)
(+ a b))
;; => PLUS
(plus)
;; => NILNIL
;;
;; 0
(plus 2)
;; => TNIL
;;
;; 2
(plus 2 2)
;; => TT
;;
;; 4
(defun plus (a b &rest rest)
(+ a b (length rest)))
;; => PLUS
(plus 1 2)
;; => 3
(plus 1 2 "a" "b" "c")
;; => 6
(defun hello-world (name &key shouty rude)
(let* ((basic (format nil "Hello ~a" name))
(postfix (if rude ", you pie!" "."))
(greeting (format nil "~a~a" basic postfix)))
(if shouty
(string-upcase greeting)
greeting)))
;; => HELLO-WORLD
(hello-world "bob")
;; => "Hello bob."
(hello-world "bob" :shouty t)
;; => "HELLO BOB."
(hello-world "bob" :rude t)
;; => "Hello bob, you pie!"
(hello-world "bob" :rude t :shouty t)
;; => "HELLO BOB, YOU PIE!"
(defun hello-everyone (&rest names &key shouty rude)
(let* ((basic (format nil "Hello ~a" names))
(postfix (if rude ", you pie!" "."))
(greeting (format nil "~a~a" basic postfix)))
(if shouty
(string-upcase greeting)
greeting)))
;; => HELLO-EVERYONE
(hello-everyone :rude t :shouty t)
;; => "HELLO (RUDE T SHOUTY T), YOU PIE!"