-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsnippets.html
122 lines (108 loc) · 5.79 KB
/
snippets.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
---
layout: default
---
<header>
<h1 class="title">Snippets</h1>
</header>
<p>One of the great resources we can have as developers is snippets and example code to understand how the idiom of a language is spoken and written.</p>
<h3 id="defining-a-function">Defining a function:</h3>
<pre class="sourceCode lisp"><code class="sourceCode lisp">(defun function-name (param1 param2 &key (keyword1 default-value))
;; keywords are optional; optional positional parameters are also available.
;; implicit progn
)
</code></pre>
<h3 id="defining-a-method-on-a-type">Defining a method on a type:</h3>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmethod method-name ((param1 class-name))
;; implicit progn
)</code></pre>
<h3 id="defining-a-class">Defining a class</h3>
<p>Note that <code>DEFCLASS</code> accessor functions for slots can be <code>SETF</code>’d and serve as both getters and setters for the slot.</p>
<p><code>:INITARG</code> is the keyword used in <code>MAKE-INSTANCE</code> to denote the value of the initial argument (see below).</p>
<p><code>:INITFORM</code> is the form used to initialize the slot. Without this, it defaults to <code>nil</code>. I favor using <code>nil</code>, <code>0</code>, <code>""</code>, or <code>(error "You must set slot <slotname> to a value")</code> as the usual initform set.</p>
<p>Note that <code>(:documentation ...)</code> is the standard documentation mechanism, which can be viewed in the running image with <code>DESCRIBE</code> (at least in SBCL).</p>
<pre class="sourceCode commonlisp"><code class="sourceCode lisp">
;; no superclass, no slots.
(defclass superclass-1 nil nil)
(defclass my-class (superclass-1)
((variable
:accessor accessor-function
:initarg :variable
:initform form-for-initializition.)
another-variable)
(:documentation "a class snippet!"))</code></pre>
<h3 id="creating-an-instance">Creating an instance</h3>
<pre class="sourceCode commonlisp"><code class="sourceCode lisp">(make-instance 'my-class :variable value)</code></pre>
<h3 id="adding-a-constructor">Adding a constructor</h3>
<p>This function runs after the instance is initialized.</p>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmethod initialize-instance :after ((obj my-class) &key)
(setf (accessor-function obj) 10))</code></pre>
<h3 id="defining-a-constant">Defining a constant:</h3>
<p>Note that the convention is that constants are surrounded with +</p>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defconstant +name+ value "docstring")</code></pre>
<h3 id="defining-a-global-variable">Defining a global variable:</h3>
<p>Note that the convention is that globals are surrounded with *</p>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defparameter *name* value "docstring")</code></pre>
<h3 id="creating-local-variables.">Creating local variables.</h3>
<pre class="sourceCode lisp"><code class="sourceCode lisp">(let ((variable1 value-form)
(variable2 value-again))
;; implicit progn where variable[12] are valid</span>
)</code></pre>
<h3 id="loop">LOOP</h3>
<p>LOOP is a contentious form in Common Lisp: some people love its imperative style, others hate it. Regardless, its really handy! Here are my favorite LOOPs</p>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
(loop for i from 0 upto 10
collect i)
(loop for i from 0 below 10
collect i)
(loop for i from 0 upto 10
do
(side-effect i))
(loop for ele in list
collect
(operate-on ele))
(loop for ele across array
collect
(operate-on ele))</code></pre>
<h3 id="lambda-functions">Lambda functions</h3>
<p>The lambda functions is an anonymous function, i.e., unnamed.</p>
<p>Here we map over <code>numeric-list</code> and increment each element in it by 1 with <code>1+</code>, returning the incremented list.</p>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
(mapcar
#'(lambda (x)
(1+ x))
numeric-list)</code></pre>
<h3 id="macro">Macro</h3>
<p>Note that Lisp macros should be used with care: they read source code and emit source code.</p>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmacro with-resource ((resource) &body body)
(allocate-resource ,resource)
(unwind-protect
(progn
,@body)
(deallocate-resource ,resource)))</code></pre>
<p>See <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_unwind.htm">UNWIND-PROTECT</a> for details on this very useful form.</p>
<h3 id="writing-a-text-file">Writing a text file</h3>
<p>More complete reading and writing of text files can be done by using the ALEXANDRIA library; these routines are great for starting to customize your own routine.</p>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defun write-file (filename data)
(with-open-file (stream
filename
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-sequence
data
stream)))</code></pre>
<h3 id="reading-a-text-file">Reading a text file</h3>
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
(defun read-file (filename)
"Reads `filename` as a sequence of unsigned 8-bit bytes, no
encoding"
(with-open-file (fin filename
:direction :input
:if-does-not-exist :error)
(let ((seq (make-array (file-length fin)
:fill-pointer t)))
(setf (fill-pointer seq)
(read-sequence seq fin))
seq)))</code></pre>
<p>Please feel free to contribute your examples or library information to this page! Send in those pull requests and file those bugs!</p>
<hr/>