Differences between current version and previous revision of QuickLispTutorial.
Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 7 | Last edited on Wednesday, April 4, 2007 10:47:35 am | by AristotlePagaltzis | |
Older page: | version 6 | Last edited on Friday, November 21, 2003 8:24:14 pm | by AristotlePagaltzis | Revert |
@@ -1,57 +1,104 @@
-!!! The basics of
LISP
+!!! [
LISP]’s very basics
-(If this this interests you
, there are some tutorial links on
the [LISP] page
.)
+''In the examples
, any bold stuff is the result of running
the code above it
.''
This is a list:
-
(apple orange banana)
+
+<verbatim>
+
(apple orange banana)
+</verbatim>
Lists can be nested:
-
((fruit (apple orange banana)) (colour (red green)))
+
+<verbatim>
+
((fruit (apple orange banana)) (colour (red green)))
+</verbatim>
Lisp usually interprets lists as function calls:
-
(+ 40 2)
-
42
+
+<pre>
+
(+ 40 2)
+<b>
42</b>
+</pre>
Lisp usually interprets symbols (i.e. identifiers) as variable references:
- (* pi 2)
- 6.2831853071795862
-A ' sign
"quotes" a
something, i.e. prevents Lisp from interpreting it. So:
-
'(elem1 elem2 elem3)
-is a list of symbols
.
+<pre>
+(* pi 2)
+<b>6.2831853071795862</b>
+</pre>
+
+
A single-quote character
"quotes" something, i.e. prevents Lisp from interpreting it. So the following is a list of symbols
:
+
+<verbatim>
+
'(elem1 elem2 elem3)
+</verbatim>
+
+<tt>let</tt> creates a list of variables that can be used in an expression:
+
+<verbatim>
+(let ((a 1) (b 2) (mylist '(a b))) expression)
+</verbatim>
+
+Hereafter, <tt>a</tt> will be 1, <tt>b</tt> = 2, <tt>mylist</tt> will contain the list <tt>'(a b)</tt>.
+
+<tt>setq</tt> changes the value of a variable:
+
+<verbatim>
+(setq mylist '(a b c))
+</verbatim>
+
+The <tt>cons</tt> function makes a pair:
+
+<verbatim>
+(cons 'first 42)
+</verbatim>
+
+Cons pairs are usually used to make lists: the first element is the first element on the list; the second element
is either
a cons pair for the rest of the
list or a <tt>nil</tt> to mark the end
of the list
. Consider the following list:
+
+<verbatim>
+'(1 2 3)
+</verbatim>
+
+This list is a shorthand for:
+
+<verbatim>
+(cons 1 (cons 2 (cons 3 nil)))
+</verbatim>
+
+<tt>car</tt> returns the first element of a list:
+
+<pre>
+(print (car mylist))
+<b>a</b>
+</pre>
+
+<tt>cdr</tt> returns the rest of the list (ie all-but-first):
+
+<pre>
+(print (cdr mylist))
+<b>(b c)</b>
+</pre>
-__let__ creates a list of variables
that can be used in an expression:
- (let ((a 1) (b 2) (mylist '(a b))) expression)
-in ''expression'' a = 1, b = 2, mylist = '(a b)
.
+And
that is basically [LISP]
.
-__setq__ changes the value of a variable.
- (setq mylist '(a b c))
+!!! A somewhat realistic example
-The __cons__
function makes a pair:
- (cons 'first 42)
-Cons pairs are usually used to make lists: the first element is the first element on the list; the second element is either a cons pair for the rest of the list or a __nil__ to mark the end of the list.
- '(1 2 3)
-is shorthand for
:
- (cons 1 (cons 2 (cons 3 nil)))
+The following example is an inefficient factorial
function:
-__car__ returns the first element of
a list:
- (print
(car mylist
))
- a
+<verbatim>
+(defun fact (x) ;
a recursive function
+ (if
(> x )
+ (* x (fact (- x 1)
))
+ 1))
+</verbatim>
-__cdr__ returns
the rest
of the list (ie all-but-first):
- (print (cdr mylist))
- (b c
)
+(This stupid function has become
the HelloWorld
of functional ProgrammingLanguage~s for some reason.
)
-And that is basically [LISP] ;)
+!!! Further reading
-The names "car" and "cdr" came from the first machine that Lisp was written
for: there were 2 registers and
the commands "contents of address register" and "contents of decrement register". CommonLisp allows
the more sensible names "first" and "rest"
.
+Look
for the tutorial links on
the [LISP] page
.
-The following example is an inefficient factorial function[1]
:
- (defun fact (x) ;a recursive function
- (if (> x )
- (* x (fact (- x 1)))
- 1
- ) )
+!!! Aside
: where do these weird names come from?
-----
-[1] this stupid function has become
the HelloWorld of functional [ProgrammingLanguage]s,
for some reason
.
+The names "<tt>car</tt>" and "<tt>cdr</tt>" came from
the first machine that Lisp was written
for: there were 2 registers and the commands "<tt>contents of address register</tt>" and "<tt>contents of decrement register</tt>". CommonLisp allows the more sensible names "<tt>first</tt>" and "<tt>rest</tt>"
.