1.html 16.7 KB
Newer Older
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
---
title: Welcome to Lisp
---

<h2>Beginner Lisp Slides 1: Welcome to Lisp</h2>

<div id="title">2.1. S-expressions</div><p></p><ul class="bullet-list"><li><div class="bullet">
Lisp source code is made up of Symbolic Expressions, or S-Expressions.

Each S-expressions is either:
<ul><li>a list</li><li>an atom</li></ul></div><span class="lisp-code"><pre>
'(2 3 4)</pre></span><pre><div class="lisp-return-value">-&gt; (2 3 4)</div></pre><span class="lisp-code"><pre>
'((&quot;red&quot; 0) (&quot;white&quot; 1) (&quot;blue&quot; 2))</pre></span><pre><div class="lisp-return-value">-&gt; ((&quot;red&quot; 0) (&quot;white&quot; 1) (&quot;blue&quot; 2))</div></pre><span class="lisp-code"><pre>
'(a &quot;b&quot; :c (1 2 3 &quot;four&quot;))</pre></span><pre><div class="lisp-return-value">-&gt; (a &quot;b&quot; :c (1 2 3 &quot;four&quot;))</div></pre><span class="lisp-code"><pre>
'a</pre></span><pre><div class="lisp-return-value">-&gt; a</div></pre><span class="lisp-code"><pre>
&quot;b&quot;</pre></span><pre><div class="lisp-return-value">-&gt; &quot;b&quot;</div></pre><span class="lisp-code"><pre>
:c</pre></span><pre><div class="lisp-return-value">-&gt; :c</div></pre></li></ul>

<div id="title">2.2. S-Expression Syntax</div><p></p><ul class="bullet-list"><li><div class="bullet">Prefix notation.</div><span class="lisp-code"><pre>
(fn arg1 arg2 arg3 &quot;...&quot; argN)</pre></span><pre><div class="lisp-return-value">-&gt; some-return-value</div></pre><span class="lisp-code"><pre>
(+ 2 3 4)</pre></span><pre><div class="lisp-return-value">-&gt; 9</div></pre><span class="lisp-code"><pre>
(fn1 (fn2 arg1 arg2) (fn3 arg3 arg4) arg5)</pre></span><pre><div class="lisp-return-value">-&gt; some-return-value</div></pre><span class="lisp-code"><pre>
(* (- 7 1) (- 4 2) 2)</pre></span><pre><div class="lisp-return-value">-&gt; 24</div></pre></li></ul>

<div id="title">2.3. Lisp Syntax Simplicity</div><p></p><ul class="bullet-list"><li><div class="bullet">That's really all there is to know about the 
syntax of the language</div></li><li><div class="bullet">It is as clean and simple as you can get.</div></li><li><div class="bullet">No ambiguities, no special cases</div></li><li><div class="bullet">A welcome relief from the syntax of other 
popular languages such as Perl, C++, etc...</div></li></ul>


<div id="title">2.4. S-expressions to be Evaluated as Code</div><p></p><ul class="bullet-list"><li><div class="bullet">When S-exps are to be evaluated at runtime, certain rules apply:</div></li><li><div class="bullet">Symbols evaluate as variables</div><span class="lisp-code"><pre>
*print-length*</pre></span><pre><div class="lisp-return-value">-&gt; nil</div></pre></li><li><div class="bullet">Other atoms (numbers, strings, keyword symbols, 
quoted symbols) evaluate to themselves</div><span class="lisp-code"><pre>
42</pre></span><pre><div class="lisp-return-value">-&gt; 42</div></pre><span class="lisp-code"><pre>
:key-1</pre></span><pre><div class="lisp-return-value">-&gt; :key-1</div></pre><span class="lisp-code"><pre>
'a</pre></span><pre><div class="lisp-return-value">-&gt; a</div></pre></li><li><div class="bullet">Unquoted lists evaluate as function calls or 
macro-expansions, driven by the symbol at the beginning 
of the list</div><span class="lisp-code"><pre>
(+ 1 2)</pre></span><pre><div class="lisp-return-value">-&gt; 3</div></pre><span class="lisp-code"><pre>
(string-upcase &quot;hey now&quot;)</pre></span><pre><div class="lisp-return-value">-&gt; &quot;HEY NOW&quot;</div></pre><span class="lisp-code"><pre>
(string-upcase
   (with-open-file (in &quot;/tmp/data.in&quot;) (read in)))</pre></span><pre><div class="lisp-return-value">-&gt; &quot;[first S-exp in text of file]&quot;</div></pre></li></ul>

<div id="title">2.5. When Arguments are S-Expressions</div><p></p><ul class="bullet-list"><li><div class="bullet">If any of the arguments are themselves expressions they 
are evaluated in the same manner</div></li><li><div class="bullet">The sub-expression (i.e. expression nested within another expression) 
is evaluated, and its result is passed as an argument</div></li><li><div class="bullet">(fn1 (fn2 arg1 arg2) (fn3 arg3 arg4) arg5)</div><span class="lisp-code"><pre>
(* (- 7 1) (- 4 2) 2)</pre></span><pre><div class="lisp-return-value">-&gt; 24</div></pre></li></ul>

<div id="title">2.6. Variable Number of Arguments</div><p></p><ul class="bullet-list"><li><div class="bullet">In Lisp - because of prefix notation</div><span class="lisp-code"><pre>
(+ 1 2 3 4 5 6 7)</pre></span><pre><div class="lisp-return-value">-&gt; 28</div></pre></li><li><div class="bullet">No ambiguity or precedence rules to remember regarding Order of Operations</div></li><li><div class="bullet">Functions potentially can take any number of arguments (including zero)</div><span class="lisp-code"><pre>
(+)</pre></span><pre><div class="lisp-return-value">-&gt; 0</div></pre><span class="lisp-code"><pre>
(+ 1)</pre></span><pre><div class="lisp-return-value">-&gt; 1</div></pre></li><li><div class="bullet">Functions can also have certain required arguments</div><span class="lisp-code"><pre>
(/ 2)</pre></span><pre><div class="lisp-return-value">-&gt; 1/2</div></pre><span class="lisp-code"><pre>
(- 2)</pre></span><pre><div class="lisp-return-value">-&gt; -2</div></pre></li><li><div class="bullet">Most modern CL development
	environments (e.g. Slime) will tell you what are the required
	arguments (if any) when you insert a function name.</div></li></ul>

<div id="title">2.7. Turning off Evaluation</div><p></p><ul class="bullet-list"><li><div class="bullet"><tt>(+ 1 2)</tt> evaluates to 3</div></li><li><div class="bullet">Sometimes you want to turn off expression evaluation</div></li><li><div class="bullet"><tt>(quote (+ 1 2))</tt> evaluates to <tt>(+ 1 2)</tt></div></li><li><div class="bullet">Common Lisp defines ' as an abbreviation for quote</div></li><li><div class="bullet"><tt>'(+ 1 2)</tt> evaluates to <tt>(+ 1 2)</tt></div></li><li><div class="bullet">Note that <tt>quote</tt> is one of a few <i>special
operators</i> in Common Lisp (you can see that it is not an ordinary
function, otherwise it would evaluate its arguments).</div></li><li><div class="bullet">Note also that lists returned by <tt>quote</tt> (unlike
lists returned by <tt>list</tt>) may be assumed to be <i>immutable</i>
-- that is, you should never modify them in place using any
destructive operators (no need to worry about this for now).</div></li></ul>

<div id="title">2.8. Lisp Data Types</div><p></p><ul class="bullet-list"><li><div class="bullet">
Usual Data Types in Other Languages
<ul><li>Numbers</li><li>Strings</li></ul></div></li><li><div class="bullet">
Fundamental Lisp Data Types
<ul><li>Symbols</li><li>Lists</li></ul></div></li></ul>

<div id="title">2.9. What are Symbols?</div><p></p><ul class="bullet-list"><li><div class="bullet">Symbols are OBJECTS</div></li><li><div class="bullet">They are NOT simply strings.</div></li><li><div class="bullet">They have a name, and can have a value, function and property-list</div></li><li><div class="bullet">'Red evaluates to Red, the <i>Symbol Name</i> of the symbol named ``Red''</div></li><li><div class="bullet">When the <i>Lisp Reader</i> encounters a Symbol for the first time, 
it <i>interns</i> (creates) this symbol in its internal <i>Symbol Table</i>.</div></li></ul>

<div id="title">2.10. Why Symbols?</div><p></p><ul class="bullet-list"><li><div class="bullet">Two references to "MyBigLongString" and "MyBigLongString"
refer to different strings</div></li><li><div class="bullet">'MySymbol and 'MySymbol are both represented by the same
symbol</div></li><li><div class="bullet"><i>eql</i> is the most basic equality function in Lisp, testing whether its arguments refer to the same 
actual <i>Object</i> (memory location), while <i>string-equal</i> compares two strings for equality, character-by-character.</div></li><li><div class="bullet">Comparing symbols for equality is much faster than comparing strings, 
since simple <i>eql</i> can be used instead of <i>string-equal</i></div><span class="lisp-code"><pre>
(string-equal &quot;Mybiglongstring&quot; &quot;Mybiglongstring&quot;)</pre></span><pre><div class="lisp-return-value">-&gt; t</div></pre><span class="lisp-code"><pre>
(eql 'MySymbol 'MySymbol)</pre></span><pre><div class="lisp-return-value">-&gt; t</div></pre></li><li><div class="bullet">Symbols turn out to be a very useful concept, and one of the distinguishing features of the Lisp language.</div></li><li><div class="bullet">More on this later</div></li></ul>


<div id="title">2.11. Lists</div><p></p><ul class="bullet-list"><li><div class="bullet">Lists are fundamental to LISP - LISt Processing</div></li><li><div class="bullet">Lists are zero or more elements enclosed by parentheses</div></li><li><div class="bullet">You have to quote a literal list for it to be evaluated
 as such; otherwise Lisp will assume you are trying to call 
a function</div><span class="lisp-code"><pre>
'(red green blue)</pre></span><pre><div class="lisp-return-value">-&gt; (red green blue)</div></pre></li></ul>

<div id="title">2.12. Lisp Programs</div><p></p><ul class="bullet-list"><li><div class="bullet">Lisp programs are themselves Lists</div></li><li><div class="bullet">It is very easy for Lisp programs to generate and execute
Lisp code</div></li><li><div class="bullet">This is NOT true of most other languages</div><span class="lisp-code"><pre>
(defun hey-now () (print &quot;Hey Now&quot;))</pre></span><pre><div class="lisp-return-value">-&gt; hey-now</div></pre></li></ul>

<div id="title">2.13. Overview of Lists - Creation</div><p></p><ul class="bullet-list"><li><div class="bullet">A literal list with single-quote (elements are <i>not</i> evaluated)</div><span class="lisp-code"><pre>
'(this is a list)</pre></span><pre><div class="lisp-return-value">-&gt; (this is a list)</div></pre></li><li><div class="bullet">Using the ``List'' function (evaluates all arguments)</div><span class="lisp-code"><pre>
(list '(+ 1 2) 'is (+ 1 2))</pre></span><pre><div class="lisp-return-value">-&gt; ((+ 1 2) is 3)</div></pre></li></ul>


<div id="title">2.14. Simple List Operations</div><p></p><ul class="bullet-list"><li><div class="bullet">first list</div><span class="lisp-code"><pre>
(first '(a b c))</pre></span><pre><div class="lisp-return-value">-&gt; a</div></pre></li><li><div class="bullet">second list</div><span class="lisp-code"><pre>
(second '(a b c))</pre></span><pre><div class="lisp-return-value">-&gt; b</div></pre></li><li><div class="bullet">third list</div><span class="lisp-code"><pre>
(third '(a b c))</pre></span><pre><div class="lisp-return-value">-&gt; c</div></pre></li><li><div class="bullet">rest list</div><span class="lisp-code"><pre>
(rest '(a b c))</pre></span><pre><div class="lisp-return-value">-&gt; (b c)</div></pre></li><li><div class="bullet">nth idx list</div><span class="lisp-code"><pre>
(nth 2 '(a b c))</pre></span><pre><div class="lisp-return-value">-&gt; c</div></pre></li></ul>


<div id="title">2.15. The Empty List</div><p></p><ul class="bullet-list"><li><div class="bullet">nil represents falsehood,</div><span class="lisp-code"><pre>
nil</pre></span><pre><div class="lisp-return-value">-&gt; nil</div></pre></li><li><div class="bullet">as well as the empty list.</div><span class="lisp-code"><pre>
(list)</pre></span><pre><div class="lisp-return-value">-&gt; nil</div></pre></li><li><div class="bullet">nil evaluates to itself</div></li></ul>



<div id="title">2.16. Testing for Listness</div><p></p><ul class="bullet-list"><li><div class="bullet">listp item</div><span class="lisp-code"><pre>
(listp '(pontiac cadillac chevrolet))</pre></span><pre><div class="lisp-return-value">-&gt; t</div></pre><span class="lisp-code"><pre>
(listp 99)</pre></span><pre><div class="lisp-return-value">-&gt; nil</div></pre></li><li><div class="bullet">nil represents FALSE</div><span class="lisp-code"><pre>
(listp nil)</pre></span><pre><div class="lisp-return-value">-&gt; &quot;???&quot;</div></pre></li></ul>


<div id="title">2.17. Functions</div><p></p><ul class="bullet-list"><li><div class="bullet">defun name arg-list forms</div><span class="lisp-code"><pre>
(defun hey-now () (print &quot;Hey Now&quot;))</pre></span><pre><div class="lisp-return-value">-&gt; hey-now</div></pre><span class="lisp-code"><pre>
(hey-now)</pre></span><pre><div class="lisp-return-value">-&gt; &quot;[prints] Hey Now&quot;</div></pre><span class="lisp-code"><pre>
(defun square (x) (* x x))</pre></span><pre><div class="lisp-return-value">-&gt; square</div></pre><span class="lisp-code"><pre>
(square 4)</pre></span><pre><div class="lisp-return-value">-&gt; 16</div></pre></li></ul>

<div id="title">2.18. Reading Lisp Code</div><p></p><ul class="bullet-list"><li><div class="bullet">Read using indentation.</div></li><li><div class="bullet">You can generally let your eyes skip over the parenthesis</div><span class="lisp-code"><pre>
(defun my-first-lisp-fn () (list 'hello 'world))</pre></span><pre><div class="lisp-return-value">-&gt; my-first-lisp-fn</div></pre></li><li><div class="bullet">Try to read the preceding example as:
<pre>
 (defun 
my-first-lisp-fn (
)(list 'hello world)
  )
</pre></div></li><li><div class="bullet">Now try to read it as:
<pre>
  defun my-first-lisp-fn ()
    list 'hello 'world
</pre></div></li><li><div class="bullet">Close all parentheses together on the line.
Dangling parenthesis are generally frowned upon in finished code.</div><span class="lisp-code"><pre>
'(united-states
  (ohio (toledo) indiana michigan
   (detroit
    (rennaissance-center
     (200-tower (fifth-floor (suite-20)))))))</pre></span></li><li><div class="bullet">Compare the above example to:
<pre>
 '(united-states 
   (ohio 
     (toledo)
     indiana
     michigan
      (detroit
       (rennaissance-center
        (200-tower
         (fifth-floor
          (suite-20)
          )
         )
        )
       )
      )
     )</pre></div></li><li><div class="bullet">(no need for these dangling parentheses)</div></li></ul>


<div id="title">2.19. Local Variables</div><p></p><ul class="bullet-list"><li><div class="bullet">New local variables are bound (introduced) using Let.</div></li><li><div class="bullet"><i>let variable-assignments body</i></div><span class="lisp-code"><pre>
(let ((quantity 20) (excess 10))
  (+ quantity excess))</pre></span><pre><div class="lisp-return-value">-&gt; 30</div></pre></li></ul>


<div id="title">2.20. Variable Assignment</div><p></p><ul class="bullet-list"><li><div class="bullet">In Lisp we use setq instead of =, :=, etc.</div><span class="lisp-code"><pre>
(let ((quantity 20) (excess 10))
  (setq quantity 30)
  (+ quantity excess))</pre></span><pre><div class="lisp-return-value">-&gt; 40</div></pre></li><li><div class="bullet">Global variables can be set with defparameter and changed with setq.</div><span class="lisp-code"><pre>
(defparameter *todays-temp* 101.5)</pre></span><pre><div class="lisp-return-value">-&gt; *todays-temp*</div></pre><span class="lisp-code"><pre>
(setq *todays-temp* 99)</pre></span><pre><div class="lisp-return-value">-&gt; 99</div></pre><span class="lisp-code"><pre>
*todays-temp*</pre></span><pre><div class="lisp-return-value">-&gt; 99</div></pre></li><li><div class="bullet">It is a long-standing Lisp convention to use Asterisks (``*'') 
to signify Global Variables (parameters). This is convention only, and the Asterisks 
have no meaning to Lisp itself.</div></li><li><div class="bullet">Setf is used to set ``locations'' resulting from a function call:</div><span class="lisp-code"><pre>
(setq *operating-systems*
      (list &quot;NT&quot; &quot;Solaris&quot; &quot;Irix&quot; &quot;HP-UX&quot; &quot;AIX&quot;))</pre></span><pre><div class="lisp-return-value">-&gt; (&quot;NT&quot; &quot;Solaris&quot; &quot;Irix&quot; &quot;HP-UX&quot; &quot;AIX&quot;)</div></pre><span class="lisp-code"><pre>
(setf (first *operating-systems*) &quot;Linux&quot;)</pre></span><pre><div class="lisp-return-value">-&gt; &quot;Linux&quot;</div></pre><span class="lisp-code"><pre>
*operating-systems*</pre></span><pre><div class="lisp-return-value">-&gt; (&quot;Linux&quot; &quot;Solaris&quot; &quot;Irix&quot; &quot;HP-UX&quot; &quot;AIX&quot;)</div></pre></li></ul>


<div id="title">2.21. Iteration</div><p></p><ul class="bullet-list"><li><div class="bullet">dolist, dotimes</div></li><li><div class="bullet"><i>dolist (variable-name list &optional return-value)</i></div><span class="lisp-code"><pre>
(let ((result 0))
  (dolist (elem '(4 5 6 7) result)
    (setq result (+ result elem))))</pre></span><pre><div class="lisp-return-value">-&gt; 22</div></pre></li><li><div class="bullet"><i>dotimes (variable-name upper-limit &optional return-value)</i></div><span class="lisp-code"><pre>
(let ((result 1))
  (dotimes (n 10 result)
    (setq result (+ result n))))</pre></span><pre><div class="lisp-return-value">-&gt; 46</div></pre></li></ul>



<hr />
Copyright &copy; 2012 <a href="http://www.genworks.com">Genworks<sup>&reg;</sup>International</a> and <a href="http://home.tudelft.nl/en/">Delft University of Technology</a>.