-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.html
253 lines (253 loc) · 19 KB
/
functions.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>functions</title>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(title);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
background-color: #2a211c;
color: #bdae9d;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #bdae9d; padding-left: 4px; }
div.sourceCode
{ color: #bdae9d; background-color: #2a211c; }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ffff00; } /* Alert */
code span.an { color: #0066ff; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { } /* Attribute */
code span.bn { color: #44aa43; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #43a8ed; font-weight: bold; } /* ControlFlow */
code span.ch { color: #049b0a; } /* Char */
code span.cn { } /* Constant */
code span.co { color: #0066ff; font-weight: bold; font-style: italic; } /* Comment */
code span.do { color: #0066ff; font-style: italic; } /* Documentation */
code span.dt { text-decoration: underline; } /* DataType */
code span.dv { color: #44aa43; } /* DecVal */
code span.er { color: #ffff00; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #44aa43; } /* Float */
code span.fu { color: #ff9358; font-weight: bold; } /* Function */
code span.im { } /* Import */
code span.in { color: #0066ff; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #43a8ed; font-weight: bold; } /* Keyword */
code span.op { } /* Operator */
code span.pp { font-weight: bold; } /* Preprocessor */
code span.sc { color: #049b0a; } /* SpecialChar */
code span.ss { color: #049b0a; } /* SpecialString */
code span.st { color: #049b0a; } /* String */
code span.va { } /* Variable */
code span.vs { color: #049b0a; } /* VerbatimString */
code span.wa { color: #ffff00; font-weight: bold; } /* Warning */
</style>
<link rel="stylesheet" href="tufte.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<h1 id="functions">Functions</h1>
<div class="sourceCode" id="cb1"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb1-1" title="1">(<span class="kw">defun</span><span class="fu"> plus </span>(a b)</a>
<a class="sourceLine" id="cb1-2" title="2"> (<span class="op">+</span> a b))</a>
<a class="sourceLine" id="cb1-3" title="3"><span class="co">;; => PLUS</span></a>
<a class="sourceLine" id="cb1-4" title="4"></a>
<a class="sourceLine" id="cb1-5" title="5">(plus <span class="dv">2</span> <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb1-6" title="6"><span class="co">;; => 7</span></a></code></pre></div>
<ul>
<li>functions are defined with the macro <code>defun</code></li>
<li>the name of a function is a symbol that’s the second form in the list</li>
<li>the arguments to the function are a list that is the third form in the list</li>
<li>everything afterwards is the body of the function</li>
</ul>
<div class="sourceCode" id="cb2"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb2-1" title="1">(<span class="kw">defun</span><span class="fu"> another-plus </span>(a b)</a>
<a class="sourceLine" id="cb2-2" title="2"> (<span class="kw">setf</span> a <span class="dv">1337</span>)</a>
<a class="sourceLine" id="cb2-3" title="3"> (<span class="op">+</span> a b))</a>
<a class="sourceLine" id="cb2-4" title="4"><span class="co">;; => ANOTHER-PLUS</span></a>
<a class="sourceLine" id="cb2-5" title="5"></a>
<a class="sourceLine" id="cb2-6" title="6">(another-plus <span class="dv">2</span> <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb2-7" title="7"><span class="co">;; => 1342</span></a></code></pre></div>
<ul>
<li>you can have multiple forms within the function body - there’s implicitly a block around them all.</li>
<li>the final form in the function body is what the function evaluates to.</li>
</ul>
<div class="sourceCode" id="cb3"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb3-1" title="1">(<span class="kw">defun</span><span class="fu"> plus </span>(a b c)</a>
<a class="sourceLine" id="cb3-2" title="2"> (<span class="op">+</span> a b c))</a>
<a class="sourceLine" id="cb3-3" title="3"><span class="co">;; => PLUS</span></a>
<a class="sourceLine" id="cb3-4" title="4"></a>
<a class="sourceLine" id="cb3-5" title="5">(plus <span class="dv">2</span> <span class="dv">5</span> <span class="dv">11</span>)</a>
<a class="sourceLine" id="cb3-6" title="6"><span class="co">;; => 18</span></a>
<a class="sourceLine" id="cb3-7" title="7"></a>
<a class="sourceLine" id="cb3-8" title="8">(plus <span class="dv">2</span> <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb3-9" title="9"><span class="co">;; ERROR!!!</span></a></code></pre></div>
<ul>
<li>there is no function overload. If we define a function with the same name, we just redefine existing function.</li>
<li>there is no currying so if don’t pass enough arguments to a function, it will just blow up</li>
</ul>
<div class="sourceCode" id="cb4"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb4-1" title="1">(<span class="kw">defun</span><span class="fu"> plus </span>(a b &optional c)</a>
<a class="sourceLine" id="cb4-2" title="2"> (<span class="kw">if</span> c (<span class="op">+</span> a b c)</a>
<a class="sourceLine" id="cb4-3" title="3"> (<span class="op">+</span> a b)))</a>
<a class="sourceLine" id="cb4-4" title="4"><span class="co">;; => PLUS</span></a>
<a class="sourceLine" id="cb4-5" title="5"></a>
<a class="sourceLine" id="cb4-6" title="6">(plus <span class="dv">3</span> <span class="dv">2</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb4-7" title="7"><span class="co">;; => 6</span></a>
<a class="sourceLine" id="cb4-8" title="8"></a>
<a class="sourceLine" id="cb4-9" title="9">(plus <span class="dv">3</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb4-10" title="10"><span class="co">;; => 5</span></a></code></pre></div>
<ul>
<li>Functions can have optional arguments denoted by a <code>&optional</code> prefix.</li>
</ul>
<div class="sourceCode" id="cb5"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb5-1" title="1">(plus <span class="dv">3</span> <span class="dv">2</span> <span class="kw">nil</span>)</a>
<a class="sourceLine" id="cb5-2" title="2"><span class="co">;; => 5</span></a></code></pre></div>
<ul>
<li>If you want to call the function without the optional parameter implicitly, just pass the nil</li>
</ul>
<div class="sourceCode" id="cb6"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb6-1" title="1">(<span class="kw">defun</span><span class="fu"> plus </span>(a b &optional (c <span class="dv">0</span>))</a>
<a class="sourceLine" id="cb6-2" title="2"> (<span class="op">+</span> a b c))</a>
<a class="sourceLine" id="cb6-3" title="3"><span class="co">;; => PLUS</span></a>
<a class="sourceLine" id="cb6-4" title="4"></a>
<a class="sourceLine" id="cb6-5" title="5">(plus <span class="dv">3</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb6-6" title="6"><span class="co">;; => 5</span></a>
<a class="sourceLine" id="cb6-7" title="7"></a>
<a class="sourceLine" id="cb6-8" title="8">(plus <span class="dv">3</span> <span class="dv">2</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb6-9" title="9"><span class="co">;; => 6</span></a></code></pre></div>
<ul>
<li>You can provide default value for the optional argument by wrapping it in the list where the second form is the default value</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb7-1" title="1">(<span class="kw">defun</span><span class="fu"> say-hello </span>(&optional (name <span class="st">"anon"</span> name-supplied-p))</a>
<a class="sourceLine" id="cb7-2" title="2"> (<span class="kw">format</span> <span class="kw">nil</span> <span class="st">"Hello ~a (~a)"</span> name name-supplied-p))</a>
<a class="sourceLine" id="cb7-3" title="3"><span class="co">;; => SAY-HELLO</span></a>
<a class="sourceLine" id="cb7-4" title="4"></a>
<a class="sourceLine" id="cb7-5" title="5">(say-hello)</a>
<a class="sourceLine" id="cb7-6" title="6"><span class="co">;; => "Hello anon (NIL)"</span></a>
<a class="sourceLine" id="cb7-7" title="7">(say-hello <span class="st">"DAVE"</span>)</a>
<a class="sourceLine" id="cb7-8" title="8"><span class="co">;; => "Hello DAVE (T)"</span></a>
<a class="sourceLine" id="cb7-9" title="9">(say-hello <span class="st">"anon"</span>)</a>
<a class="sourceLine" id="cb7-10" title="10"><span class="co">;; => "Hello anon (T)"</span></a></code></pre></div>
<ul>
<li>a third item in the <code>&optional</code> argument list is a flag to tell you whether an optional argument was actually passed</li>
<li>this is good to distinguish between the default argument being used and the same value being passed in.</li>
</ul>
<div class="sourceCode" id="cb8"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb8-1" title="1">(<span class="kw">defun</span><span class="fu"> plus </span>(&optional (a <span class="dv">0</span> a-p) (b <span class="dv">0</span> b-p))</a>
<a class="sourceLine" id="cb8-2" title="2"> (<span class="kw">princ</span> a-p)</a>
<a class="sourceLine" id="cb8-3" title="3"> (<span class="kw">princ</span> b-p)</a>
<a class="sourceLine" id="cb8-4" title="4"> (<span class="op">+</span> a b))</a>
<a class="sourceLine" id="cb8-5" title="5"><span class="co">;; => PLUS</span></a>
<a class="sourceLine" id="cb8-6" title="6"></a>
<a class="sourceLine" id="cb8-7" title="7">(plus)</a>
<a class="sourceLine" id="cb8-8" title="8"><span class="co">;; => NILNIL</span></a>
<a class="sourceLine" id="cb8-9" title="9"><span class="co">;;</span></a>
<a class="sourceLine" id="cb8-10" title="10"><span class="co">;; 0</span></a>
<a class="sourceLine" id="cb8-11" title="11"></a>
<a class="sourceLine" id="cb8-12" title="12">(plus <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb8-13" title="13"><span class="co">;; => TNIL</span></a>
<a class="sourceLine" id="cb8-14" title="14"><span class="co">;;</span></a>
<a class="sourceLine" id="cb8-15" title="15"><span class="co">;; 2</span></a>
<a class="sourceLine" id="cb8-16" title="16"></a>
<a class="sourceLine" id="cb8-17" title="17">(plus <span class="dv">2</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb8-18" title="18"><span class="co">;; => TT</span></a>
<a class="sourceLine" id="cb8-19" title="19"><span class="co">;;</span></a>
<a class="sourceLine" id="cb8-20" title="20"><span class="co">;; 4</span></a></code></pre></div>
<ul>
<li>You can have multiple optional arguments with the supplied flag bindings</li>
<li>All the arguments after <code>&optional</code> become optional</li>
</ul>
<div class="sourceCode" id="cb9"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb9-1" title="1">(<span class="kw">defun</span><span class="fu"> plus </span>(a b &<span class="kw">rest</span> <span class="kw">rest</span>)</a>
<a class="sourceLine" id="cb9-2" title="2"> (<span class="op">+</span> a b (<span class="kw">length</span> <span class="kw">rest</span>)))</a>
<a class="sourceLine" id="cb9-3" title="3"><span class="co">;; => PLUS</span></a>
<a class="sourceLine" id="cb9-4" title="4"></a>
<a class="sourceLine" id="cb9-5" title="5">(plus <span class="dv">1</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb9-6" title="6"><span class="co">;; => 3</span></a>
<a class="sourceLine" id="cb9-7" title="7"></a>
<a class="sourceLine" id="cb9-8" title="8">(plus <span class="dv">1</span> <span class="dv">2</span> <span class="st">"a"</span> <span class="st">"b"</span> <span class="st">"c"</span>)</a>
<a class="sourceLine" id="cb9-9" title="9"><span class="co">;; => 6</span></a></code></pre></div>
<ul>
<li>there is <code>&rest</code> prefix which collects all other arguments and puts them into a list</li>
</ul>
<h3 id="keyword-arguments">Keyword arguments</h3>
<div class="sourceCode" id="cb10"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb10-1" title="1">(<span class="kw">defun</span><span class="fu"> hello-world </span>(name &key shouty rude)</a>
<a class="sourceLine" id="cb10-2" title="2"> (<span class="kw">let*</span> ((basic (<span class="kw">format</span> <span class="kw">nil</span> <span class="st">"Hello ~a"</span> name))</a>
<a class="sourceLine" id="cb10-3" title="3"> (postfix (<span class="kw">if</span> rude <span class="st">", you pie!"</span> <span class="st">"."</span>))</a>
<a class="sourceLine" id="cb10-4" title="4"> (greeting (<span class="kw">format</span> <span class="kw">nil</span> <span class="st">"~a~a"</span> basic postfix)))</a>
<a class="sourceLine" id="cb10-5" title="5"> (<span class="kw">if</span> shouty</a>
<a class="sourceLine" id="cb10-6" title="6"> (<span class="kw">string-upcase</span> greeting)</a>
<a class="sourceLine" id="cb10-7" title="7"> greeting)))</a>
<a class="sourceLine" id="cb10-8" title="8"><span class="co">;; => HELLO-WORLD</span></a>
<a class="sourceLine" id="cb10-9" title="9"></a>
<a class="sourceLine" id="cb10-10" title="10">(hello-world <span class="st">"bob"</span>)</a>
<a class="sourceLine" id="cb10-11" title="11"><span class="co">;; => "Hello bob."</span></a>
<a class="sourceLine" id="cb10-12" title="12"></a>
<a class="sourceLine" id="cb10-13" title="13">(hello-world <span class="st">"bob"</span> :shouty <span class="kw">t</span>)</a>
<a class="sourceLine" id="cb10-14" title="14"><span class="co">;; => "HELLO BOB."</span></a>
<a class="sourceLine" id="cb10-15" title="15"></a>
<a class="sourceLine" id="cb10-16" title="16">(hello-world <span class="st">"bob"</span> :rude <span class="kw">t</span>)</a>
<a class="sourceLine" id="cb10-17" title="17"><span class="co">;; => "Hello bob, you pie!"</span></a>
<a class="sourceLine" id="cb10-18" title="18"></a>
<a class="sourceLine" id="cb10-19" title="19">(hello-world <span class="st">"bob"</span> :rude <span class="kw">t</span> :shouty <span class="kw">t</span>)</a>
<a class="sourceLine" id="cb10-20" title="20"><span class="co">;; => "HELLO BOB, YOU PIE!"</span></a></code></pre></div>
<ul>
<li>keyword arguments are introduced by using <code>&key</code></li>
<li>they are a way of providing named arguments in any order</li>
<li>keywords <em>have</em> to be used if it is a keyword argument</li>
<li>the <code>&key</code> appears only <em>once</em> - everything afterwards is a keyword argument</li>
<li>they default to <code>nil</code> but can have a default supplied in the same manner as <code>&optional</code> arguments</li>
</ul>
<div class="sourceCode" id="cb11"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb11-1" title="1"></a>
<a class="sourceLine" id="cb11-2" title="2">(<span class="kw">defun</span><span class="fu"> hello-everyone </span>(&<span class="kw">rest</span> names &key shouty rude)</a>
<a class="sourceLine" id="cb11-3" title="3"> (<span class="kw">let*</span> ((basic (<span class="kw">format</span> <span class="kw">nil</span> <span class="st">"Hello ~a"</span> names))</a>
<a class="sourceLine" id="cb11-4" title="4"> (postfix (<span class="kw">if</span> rude <span class="st">", you pie!"</span> <span class="st">"."</span>))</a>
<a class="sourceLine" id="cb11-5" title="5"> (greeting (<span class="kw">format</span> <span class="kw">nil</span> <span class="st">"~a~a"</span> basic postfix)))</a>
<a class="sourceLine" id="cb11-6" title="6"> (<span class="kw">if</span> shouty</a>
<a class="sourceLine" id="cb11-7" title="7"> (<span class="kw">string-upcase</span> greeting)</a>
<a class="sourceLine" id="cb11-8" title="8"> greeting)))</a>
<a class="sourceLine" id="cb11-9" title="9"><span class="co">;; => HELLO-EVERYONE</span></a>
<a class="sourceLine" id="cb11-10" title="10"></a>
<a class="sourceLine" id="cb11-11" title="11">(hello-everyone :rude <span class="kw">t</span> :shouty <span class="kw">t</span>)</a>
<a class="sourceLine" id="cb11-12" title="12"><span class="co">;; => "HELLO (RUDE T SHOUTY T), YOU PIE!"</span></a></code></pre></div>
<ul>
<li>You can’t combine <code>rest</code> arguments with named arguments. You can see here that <code>:rest</code> is “slurping” our keywords <em>and</em> the keywords are still being used.</li>
</ul>
<div class="sourceCode" id="cb12"><pre class="sourceCode commonlisp"><code class="sourceCode commonlisp"><a class="sourceLine" id="cb12-1" title="1">(<span class="kw">defun</span><span class="fu"> plus </span>(a b)</a>
<a class="sourceLine" id="cb12-2" title="2"> <span class="st">"Plus some numbers"</span></a>
<a class="sourceLine" id="cb12-3" title="3"> (<span class="op">+</span> a b))</a>
<a class="sourceLine" id="cb12-4" title="4"><span class="co">;; => PLUS</span></a>
<a class="sourceLine" id="cb12-5" title="5"></a>
<a class="sourceLine" id="cb12-6" title="6">(<span class="kw">documentation</span> 'plus 'function)</a>
<a class="sourceLine" id="cb12-7" title="7"><span class="co">;; => "Plus some numbers"</span></a></code></pre></div>
<ul>
<li>You can add handy documentation to your functions</li>
</ul>
</body>
</html>