@@ -7,107 +7,116 @@ <h1 class="title">Snippets</h1>
7
7
</ header >
8
8
< 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 >
9
9
< h3 id ="defining-a-function "> Defining a function:</ h3 >
10
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class ="kw "> defun</ span > < span class ="fu "> function-name </ span > (param1 param2 &key (keyword1 default-value))
11
- < span class ="co "> ;; keywords are optional; optional positional parameters are also available. </ span >
12
- < span class ="co "> ;; implicit progn</ span >
13
- )</ code > </ pre >
10
+ < pre class ="sourceCode lisp "> < code class ="sourceCode lisp "> (defun function-name (param1 param2 &key (keyword1 default-value))
11
+ ;; keywords are optional; optional positional parameters are also available.
12
+ ;; implicit progn
13
+ )
14
+ </ code > </ pre >
15
+
14
16
< h3 id ="defining-a-method-on-a-type "> Defining a method on a type:</ h3 >
15
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class =" kw " > defmethod</ span > < span class =" fu " > method-name </ span > ((param1 < span class =" kw " > class -name</ span > ))
16
- < span class =" co " > ;; implicit progn</ span >
17
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (defmethodmethod-name ((param1 class-name))
18
+ ;; implicit progn
17
19
)</ code > </ pre >
20
+
18
21
< h3 id ="defining-a-class "> Defining a class</ h3 >
19
22
< 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 >
20
23
< p > < code > :INITARG</ code > is the keyword used in < code > MAKE-INSTANCE</ code > to denote the value of the initial argument (see below).</ p >
21
24
< 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 >
22
25
< 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 >
23
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp ">
24
- < span class =" co " > ;; no superclass, no slots.</ span >
25
- (< span class =" kw " > defclass</ span > < span class =" fu " > superclass-1 </ span > < span class =" kw " > nil</ span > < span class =" kw " > nil</ span > )
26
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode lisp ">
27
+ ;; no superclass, no slots.
28
+ (defclasssuperclass-1 nilnil)
26
29
27
- (< span class =" kw " > defclass</ span > < span class =" fu " > my-class </ span > (superclass< span class =" dv " > -1 </ span > )
30
+ (defclassmy-class (superclass-1 )
28
31
((variable
29
32
:accessor accessor-function
30
33
:initarg :variable
31
34
:initform form-for-initializition.)
32
35
another-variable)
33
- (:documentation < span class ="st "> "a class snippet!"</ span > ))</ code > </ pre >
36
+ (:documentation "a class snippet!"))</ code > </ pre >
37
+
34
38
< h3 id ="creating-an-instance "> Creating an instance</ h3 >
35
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class ="kw "> make-instance</ span > 'my-class :variable value)</ code > </ pre >
39
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode lisp "> (make-instance 'my-class :variable value)</ code > </ pre >
40
+
36
41
< h3 id ="adding-a-constructor "> Adding a constructor</ h3 >
37
42
< p > This function runs after the instance is initialized.</ p >
38
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class ="kw "> defmethod</ span > < span class ="fu "> initialize-instance </ span > :after ((obj my-class) &key)
39
- (< span class ="kw "> setf</ span > (accessor-function obj) < span class ="dv "> 10</ span > ))</ code > </ pre >
43
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (defmethod initialize-instance :after ((obj my-class) &key)
44
+ (setf (accessor-function obj) 10))</ code > </ pre >
45
+
40
46
< h3 id ="defining-a-constant "> Defining a constant:</ h3 >
41
47
< p > Note that the convention is that constants are surrounded with +</ p >
42
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class ="kw "> defconstant</ span > < span class ="fu "> +name+ </ span > value < span class ="st "> "docstring"</ span > )</ code > </ pre >
48
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (defconstant +name+ value "docstring")</ code > </ pre >
49
+
43
50
< h3 id ="defining-a-global-variable "> Defining a global variable:</ h3 >
44
51
< p > Note that the convention is that globals are surrounded with *</ p >
45
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class ="kw "> defparameter</ span > < span class ="fu "> *name* </ span > value < span class ="st "> "docstring"</ span > )</ code > </ pre >
52
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (defparameter *name* value "docstring")</ code > </ pre >
53
+
46
54
< h3 id ="creating-local-variables. "> Creating local variables.</ h3 >
47
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class =" kw " > let</ span > ((variable1 value-form)
55
+ < pre class ="sourceCode lisp "> < code class ="sourceCode lisp "> (let ((variable1 value-form)
48
56
(variable2 value-again))
49
- < span class =" co " > ;; implicit progn where variable[12] are valid</ span >
57
+ ;; implicit progn where variable[12] are valid</ span >
50
58
)</ code > </ pre >
59
+
51
60
< h3 id ="loop "> LOOP</ h3 >
52
61
< 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 >
53
62
< pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp ">
54
- (< span class =" kw " > loop</ span > for i from < span class =" dv " > 0 </ span > upto < span class =" dv " > 10 </ span >
63
+ (loop for i from 0 upto 10
55
64
collect i)
56
65
57
- (< span class =" kw " > loop</ span > for i from < span class =" dv " > 0 </ span > below < span class =" dv " > 10 </ span >
66
+ (loop for i from 0 below 10
58
67
collect i)
59
68
60
- (< span class =" kw " > loop</ span > for i from < span class =" dv " > 0 </ span > upto < span class =" dv " > 10 </ span >
61
- < span class =" kw " > do </ span >
69
+ (loop for i from 0 upto 10
70
+ do
62
71
(side-effect i))
63
72
64
- (< span class =" kw " > loop</ span > for ele in < span class =" kw " > list</ span >
73
+ (loop for ele in list
65
74
collect
66
75
(operate-on ele))
67
76
68
- (< span class =" kw " > loop</ span > for ele across < span class =" kw " > array</ span >
77
+ (loop for ele across array
69
78
collect
70
79
(operate-on ele))</ code > </ pre >
71
80
< h3 id ="lambda-functions "> Lambda functions</ h3 >
72
81
< p > The lambda functions is an anonymous function, i.e., unnamed.</ p >
73
82
< 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 >
74
83
< pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp ">
75
- (< span class =" kw " > mapcar</ span >
76
- #'(< span class =" kw " > lambda</ span > (x)
77
- (< span class =" kw " > 1+ </ span > x))
84
+ (mapcar
85
+ #'(lambda (x)
86
+ (1+ x))
78
87
numeric-list)</ code > </ pre >
79
88
< h3 id ="macro "> Macro</ h3 >
80
89
< p > Note that Lisp macros should be used with care: they read source code and emit source code.</ p >
81
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class =" kw " > defmacro</ span > < span class =" fu " > with-resource </ span > ((resource) &body body)
90
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (defmacrowith-resource ((resource) &body body)
82
91
(allocate-resource ,resource)
83
- (< span class =" kw " > unwind-protect</ span >
84
- (< span class =" kw " > progn</ span >
92
+ (unwind-protect
93
+ (progn
85
94
,@body)
86
95
(deallocate-resource ,resource)))</ code > </ pre >
87
96
< 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 >
88
97
< h3 id ="writing-a-text-file "> Writing a text file</ h3 >
89
98
< 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 >
90
- < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (< span class =" kw " > defun</ span > < span class =" fu " > write-file </ span > (filename data)
91
- (< span class =" kw " > with-open-file</ span > ( < span class =" kw " > stream</ span >
99
+ < pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp "> (defunwrite-file (filename data)
100
+ (with-open-file ( stream
92
101
filename
93
- < span class =" kw " > :direction</ span > < span class =" kw " > :output</ span >
94
- :if-exists < span class =" kw " > :supersede</ span >
95
- :if-does-not-exist < span class =" kw " > :create</ span > )
96
- (< span class =" kw " > write-sequence</ span >
102
+ :direction:output
103
+ :if-exists :supersede
104
+ :if-does-not-exist :create)
105
+ (write-sequence
97
106
data
98
- < span class =" kw " > stream</ span > )))</ code > </ pre >
107
+ stream)))</ code > </ pre >
99
108
< h3 id ="reading-a-text-file "> Reading a text file</ h3 >
100
109
< pre class ="sourceCode commonlisp "> < code class ="sourceCode commonlisp ">
101
- (< span class =" kw " > defun</ span > < span class =" fu " > read-file </ span > (filename)
102
- < span class =" st " > "Reads `filename` as a sequence of unsigned 8-bit bytes, no</ span >
103
- < span class =" st " > encoding"</ span >
104
- (< span class =" kw " > with-open-file</ span > (fin filename
105
- < span class =" kw " > :direction</ span > < span class =" kw " > :input</ span >
106
- :if-does-not-exist < span class =" kw " > :error</ span > )
107
- (< span class =" kw " > let</ span > ((seq (< span class =" kw " > make-array</ span > ( < span class =" kw " > file-length</ span > fin)
108
- :fill-pointer < span class =" kw " > t </ span > )))
109
- (< span class =" kw " > setf</ span > ( < span class =" kw " > fill-pointer</ span > seq)
110
- (< span class =" kw " > read-sequence</ span > seq fin))
110
+ (defunread-file (filename)
111
+ "Reads `filename` as a sequence of unsigned 8-bit bytes, no
112
+ encoding"
113
+ (with-open-file (fin filename
114
+ :direction:input
115
+ :if-does-not-exist :error)
116
+ (let ((seq (make-array ( file-length fin)
117
+ :fill-pointer t )))
118
+ (setf ( fill-pointer seq)
119
+ (read-sequence seq fin))
111
120
seq)))</ code > </ pre >
112
121
< p > Please feel free to contribute your examples or library information to this page! Send in those pull requests and file those bugs!</ p >
113
122
< hr />
0 commit comments