416x Filetype PDF File size 0.14 MB Source: students.mimuw.edu.pl
Common Lisp: First Contact
Common Lisp: First Contact is an easy to read introduction to Learning to talk
Lisp. The goal is to make you familiar with Lisp syntax and with how
Lisp works. Nothing more than a basic understanding of computing Lisp is a computer language. It allows you to instruct a computer to
is required. do or to compute things. You type text representing Lisp as input to
the computer. The computer will do or compute what you typed. It
This is not an complete introduction in computer programming using 2
will print text representing Lisp as output .
1
Common Lisp .
Lisp knows about many things. For example, it knows about many
Learning to talk different kinds of numbers.
Making lists > 123
It's more fun to compute 123
Infinite combinations > 9.99
Remember me 9.99
And I quote > 1/3
1/3
Powerful spells
> 1000000000000000000000000000000000000000000
Define that, please 1000000000000000000000000000000000000000000
Powerful spells revisited If you type a number like 123, Lisp will simply return it: a number
Where to go from here ? stands for itself. Whole numbers (integers), fractional numbers (reals
Appendix 1: Getting Common Lisp or floating points), pure fractions (rationals), some with infinite
precision are just a selection of the possibilities. Most of the time,
Appendix 2: The cl-1st-contact library you will work with any combination of numbers: the computer will
Bibliography keep track of the details.
Common Lisp: First Contact • Copyright © 2007,2008 Sven Van Caekenberghe. All Rights Reserved. • Page 1 of 15 • Version 1.3 • April 18 2008
Another primitive kind of thing Lisp knows about are text strings. Lisp uses lists for two purposes: to build data structures and to
represent programs. This unique aspect gives Lisp great power and
> "Hello there!" flexibility. Say you want to write down, represent, record or store the
"Hello there!" following data:
Strings start and end with a double quote and contain all the text firstname = John
3 •
inbetween. Strings too, stand for themselves . lastname = McCarthy
•
• birthday = 19270904
Making lists
The shortest solution would be to agree to always keep the data in
To represent anything more complicated, Lisp uses one very simple this order and simply list it:
yet very flexible concept: the list. A list is an ordered collection of
things, written as a sequence of elements, separated by spaces and ("John" "McCarthy" "19270904")
surrounded by an opening and closing parenthesis.
Although short and efficient, it might be error-prone and hard to
For example, extend. In Lisp we often add a descriptive tag to each data element:
(1 2 3) ((firstname "John")
(lastname "McCarthy")
is a list containing three elements, the numbers 1, 2 and 3. Another (birthday "19270904"))
example, or
("abc" "def" "ghi") (firstname "John"
is again a list of three elements, this time containing the strings lastname "McCarthy"
birthday "19270904")
"abc", "def" and "ghi".
4
All three solutions are frequently used . Notice how we split the lists
Part of the power of lists comes from the fact that the elements can across a number of lines and how we indent succesive lines so that
be anything, including other lists. we get vertical alignment of corresponding elements: this is
important to improve readability.
For example,
((1 2 3) ("abc" "def" "ghi"))
is a list of two elements: the first of these elements is another list,
containing the numbers 1, 2 and 3, the second of these elements is
also another list, containing the strings "abc", "def" and "ghi".
Common Lisp: First Contact • Copyright © 2007,2008 Sven Van Caekenberghe. All Rights Reserved. • Page 2 of 15 • Version 1.3 • April 18 2008
> (oddp 7)
It's more fun to compute T
To add two numbers, like 1 and 2 together, as in 1 + 2, we write the > (evenp 7)
following Lisp code: NIL
> (+ 1 2) > (= 7 7)
3 T
When Lisp sees a list, it will generally look at its first element to The special value T stands for 'true' in Lisp. The special value NIL
determine what to do. In this case, it sees the plus sign which it stands for 'false' in Lisp.
knows it refers to the procedure for addition. The other two elements
in the list are seen as the arguments, the numbers to be added There is much more to Lisp than just mathematics though. Assuming
together. Lisp will look at each argument to find out what it means. we have a file named 'test.txt' in the '/tmp' directory on our computer,
Numbers stand for themselves. we can do some file manipulation as follows:
Actually, many Lisp functions and procedures accept any number of > (probe-file "/tmp/test.txt")
arguments, if that makes sense: #P"/tmp/test.txt"
> (+ 10 20 30 40 50) > (delete-file "/tmp/test.txt")
150 #P"/tmp/test.txt"
Common Lisp in its standard definition contains well over a thousand > (delete-file "/tmp/test.txt")
different functions and procedures. Here are some examples: NIL
The function PROBE-FILE tests whether a certain file exists. If it
> (* 7 7) exists it returns how it would internally represent the file's pathname.
49 In Common Lisp, everything that is not NIL is considered to be 'true',
> (expt 7 2) that is why PROBE-FILE is also a predicate: by returning something
49 that might also be useful in other cases it indicates that it actually
found the file to exist.
> (sqrt 49)
7 The function DELETE-FILE will delete a file, if it exists. If the deletion
A special class of functions are predicates: these test whether a was succesful, i.e. the file existed and you are allowed to delete it,
certain property is true or not. Conventionally, most of them have the Lisp will return 'true' by returning the interal file's pathname. Deleting
letter p appended to their name: a file that doesn't exist simply returns NIL for 'false'.
Common Lisp: First Contact • Copyright © 2007,2008 Sven Van Caekenberghe. All Rights Reserved. • Page 3 of 15 • Version 1.3 • April 18 2008
> (* 2 (+ 4 6))
Infinite combinations > 2
It is also possible to combine several computations, by simply 2
nesting them. If we want to compute (5 + 5) * (10 + 10) we write: > (+ 4 6)
10
20
> (* (+ 5 5) (+ 10 10))
200 As you can see, in order to do the multiplication, the addition(s) must
On the other hand, to compute 2 * (4 + 6) we write: be computed. In order to do the addition(s), the meaning of the
numbers has to be determined. Numbers stand for themselves.
> (* 2 (+ 4 6)) We already saw that some functions accept any number of
20
arguments, like:
When Lisp sees either of the previous two examples it knows that it
has to do a multiplication, by looking at the first element. Next it has > (< 1 2 3 4)
to deal with the arguments. This is done by computing the meaning T
each one. This is necessary because we need numbers to multiply. If > (- 100 5 5 5 5)
an argument is again a list, the process starts all over. 80
We can write out explicitly what goes on inside Lisp for each > (max 34 7 88 12)
computation above: 88
> (* (+ 5 5) (+ 10 10)) > (min 34 7 88 12)
> (+ 5 5) 7
> 5
5 > (- 10)
> 5 -10
5
10 > (+ 10)
> (+ 10 10) 10
> 10
10 > (+)
> 10 0
10
20
200
Common Lisp: First Contact • Copyright © 2007,2008 Sven Van Caekenberghe. All Rights Reserved. • Page 4 of 15 • Version 1.3 • April 18 2008
no reviews yet
Please Login to review.