Contents | Preface | Lists, CAR and CDR
We assume that you are sitting in front of your computer, with Lisp up and running [see the explanation of how to do this in the P-Lisp Manual]. You should see the following at the top of the screen:
GNOSIS INC. P-LISP VER. 3.1.2 ------------------- COPYRIGHT 1982 BY STEVEN CHERRY ALL RIGHTS RESERVEDWhen you see this display it means that you have successfully entered Lisp. The ":" prompt that you see on the last line typed by the computer means that Lisp is waiting for you to type something in. You may type in what you wish. After you hit the RETURN key Lisp will evaluate your command and display the result. This process of "read-evaluate-print" result constitutes the core of the interactive Lisp system. We will see more or READ-EVALUATE-PRINT much later on.
Note that if you hit RETURN several times, each time Lisp will respond with the ":". You told it to do nothing, so it did nothing and then asked for another line of input.
If we type a number, then Lisp will echo the number back. All of our inputs follow the ":" prompt; all of Lisp's responses are preceded by two spaces.
:3 3 :0 0 :-2 -2Let's try an example: adding up some numbers. To add numbers in Lisp we use the ADD function. We add 1 and 2 by typing:
:(ADD 1 2) 3Yeah! Lisp can add. What actually happened? Lisp typed the ":" and then we typed "(add 1 2)". Note several things:
:(ADD 12 3 :) 15 :(ADD 1) ** ERROR: TOO FEW ARGS ** ADD :: (1 ) +() NIL :(ADD 11 8 3) ** ERROR: TOO MANY ARGS ** ADD :: (11 8 3 ) +() NILHere we first try the same example but we have forgotten the closing parenthesis. Lisp is waiting for that closing parenthesis so it comes back with a ":" prompt. We enter the closing parentheses, and now Lisp is happy, so it performs the addition. In general, you may spread the input across as many lines as you like. Later this will be quite useful.
The next line shows something a little funny. We asked Lisp to add up one number. Because adding up just one number is not particularly meaningful or useful, Lisp returns an error message that there are too few numbers to add. This is quite reasonable, since you usually want to add up at least two numbers.
Note that Lisp now gives us a "+" instead of the usual ":". Don't worry about this for now, simply type "()". We will deal with this mode of operation later.
The last line shows what happens if you try to add up three numbers--the same sort of error! Well, there is no penalty for mistakes [we won't tell]. It makes a little more sense to add up more than two numbers than it did to add up just one number. We'll see much later that we can actually fix ADD ourselves to do this [or any other sort of behavior that we like].
The first thing in a pair of parentheses is the function name and the things after that are the arguments [thus the statement "TOO FEW ARGS" in the above error report]. This is very important, and these two words will be used throughout this book. In the first example above the function name is ADD and its arguments are "1" and "2". ADD is said to have two arguments in this example.
Besides addition, Lisp can also perform multiplication. The name of the multiplication function is MULT. Let's try it out!
:(MULT 2 3) 6 :(MULT 9 2) 18 :(MULT 1 2 3 4) ** ERROR: TOO MANY ARGS ** MULT :: (1 2 3 4 ) +() NIL :(MULT 1.2 4) 4.8 :(MULT 2 (ADD 1 2)) 6The first two examples reassure us that Lisp can, in fact, multiply.
Lisp can, however, multiply only two values. If you try and multiply more than two values, you will get a TOO MANY ARGS error. By the way, if you try and use MULT with zero or one argument, you will get a TOO FEW ARGS error. Again, we type "()"" to get back to the normal colon prompt.
The next example shows that Lisp will deal with non-integers. Floating point math is nice, but not critical because, as we will soon see, Lisp's strength does not lie in arithmetic.
The last of the above lines is the most interesting. Lisp tries to perform the MULT function but finds that in place of the second argument is a subexpression. The value of the subexpression "(add 1 2)" is, of course, 3. There is now a number to take the place of the subexpression so the multiplication can continue. Lisp now effectively sees "(mult 2 3)" which it performs.
Since this type of operation is very common in Lisp work, we are going to try some more examples like the last one. See if you can figure out what is happening in each expression.
:(ADD (MULT 3 4) (MULT 2 6)) 24 :(MULT (MULT (ADD 1 0) (ADD 1 1)) (MULT (ADD 2 1) (ADD 1 3))) 24 :(MULT 1 (MULT 2 (MULT 3 (MULT 4 1))))))))))) 24One important thing to notice about these examples is that in the last one there were too many closing parentheses. This is fine and, in fact is very handy sometimes when you lose count. All you need to do is keep typing lots of closing parentheses and eventually you'll get back to the colon prompt.
Now for one more concept: predicates. A predicate is a special kind of function that returns an answer of either true or false. In Lisp, true is represented as "T" and false is represented as "NIL". So, let's ask some questions.
:(GREATER 3 4) NIL :(GREATER 4 3) T :(GREATER 100 -100) T :(NUMBER 47) T :(NUMBER 'LETTERS) NIL :(NUMBER 'SEVEN') NIL :(ZERO 0) T :(ZERO (ADD 2 -1)) NIL :(ZERO (ADD 2 -2)) TThe predicate GREATER returns a true "T" if the numbers are in a strictly decreasing order; false, "NIL", otherwise. The predicate NUMBER says "T" if the argument is a number, "NIL", otherwise. Obviously the word "seven" is characters [more on what that quote in front of it means later] and is not a number. ZERO returns "T" if the argument evaluates to zero.
Contents | Preface | Lists, CAR and CDR