Learning Lisp Fast - Part 3
(Source and copyright: Sean Luke - George Mason University, cs.gmu.edu/~sean/lisp/LispTutorial3.html)
Yet more Lisp!
As before, the table cell below shows what you type, and the output, for this tutorial. Text shown in blue you are responsible for typing, with a Return at the end of the line. Text shown in black indicate stuff that is printed back to you. Text shown in red are remarks -- do not type them.
If the cell is divided by a line, as is shown at right, then this indicates two different examples.
This text is being printed out. You would type this text [This is a remark]
Lisp's primary data type is the list.
A list is a linked list elements. The linking structures are called cons cells. A cons cell is a structure with two fields: car and cdr. The car field points to the element the cons cell is holding. The cdr field points to the next cons cell, or to nil if the cons cell is at the end of the list.
When you are storing a list, you are really storing a pointer to the first cons cell in the list. Thus car (first) returns the thing that cons cell is pointing to, and cdr (rest) returns the next cons cell (what the cdr is pointing to). Similarly, the cons function allocates a new cons cell, sets its cdr to the cons cell representing the original list, and sets its car to the element you're tacking onto the list. Thus the original list isn't modified at all! You've just made a new cons cell which reuses the list to "extend" it by one.
last returns (as a list) the last n elements in the list. Really it just marches down the list and finds the appropriate cons and returns that to you. last's argument is optional -- if it's not there, it returns (as a list) the last element in the list.
butlast returns (as a list) a copy of everything but the last n elements in the list.
list takes some n arguments and makes a list out of them. It differs from just quoting a list because the arguments are evaluated first (it's a function).
> (last '(a b c d e)) (E) > (last '(a b c d e) 3) (C D E) > (butlast '(a b c d e)) (A B C D) > (butlast '(a b c d e) 3) (A B) > (list 1 2 (+ 3 2) "hello") (1 2 5 "hello") > '(1 2 (+ 3 2) "hello") (1 2 (+ 3 2) "hello")
nth works on lists just like elt. You can use it in setf. For lists, it's a tiny bit faster than (the more general function) elt. Note that nth's arguments are not the same order as those for elt. This is historical.
listp is a predicate which returns true if its argument is a list.
atom is a predicate which returns true if its argument is an atom. Note that nil is both a list and an atom. It's the only thing which is both.
consp is a predicate which is true if its argument is a list but is not nil.
null is a predicate which returns true if its argument is nil.
If you think about it, the logical not function is identical to the null function.
There are a great many more list functions. See "conses" in the text.
> (listp '(4 3 8)) T > (listp nil) T > (consp '(4 3 8)) T > (consp nil) NIL > (atom 'a) T > (atom nil) T > (null nil) T > (not nil) T > (nth 4 '(a b c d e)) E
There are a great many permuations of cdr and car.
caar is the same as (car (car ... ))
cdar is the same as (cdr (car ... ))
caadr is the same as (car (car (cdr ... )))
cddddr is the same as (cdr (cdr (cdr (cdr ... ))))
second through tenth return the second through the tenth elements of a list.
> (caar '((a b c) ((d)) e f g h)) A > (cdar '((a b c) ((d)) e f g h)) (B C) > (caadr '((a b c) ((d)) e f g h)) (D) > (cddddr '((a b c) ((d)) e f g h)) (G H) > (fourth '((a b c) ((d)) e f g h)) F
The cdr field of a cons cell doesn't have to point to a list (or to nil). Technically, it can point to anything you like. For example, a single cons cell can point to 45 car and "hello" in its cdr. Such a cons cell is called a dotted pair, because its printed form is (45 . "hello"). Note the period.
You can construct long lists where the last item doesn't point to nil but instead is a dotted pair.
If you think about it, the dotted pair can only be at the end of a list.
Dotted pairs qualify as lists as far as listp is concerned.
> (cons 45 "hello") (45 . "hello") > (cons 'a (cons 'b (cons 45 "hello"))) (A B 45 . "hello") > (listp '(45 . "hello")) T
Lists are constructed like beads of pearls. So far we've been careful not to damage them.
setf can be used to do evil things to a list.
You can use setf to modify a list. Since we construct lists which point to original lists, if we modify a list then it might modify other lists which pointed to it.
One particular danger is to use setf to modify cons cells to have circular references. For example, you could use setf to set the cdr of a cell point to the same cell.
Most Lisp systems are smart enough to detect circular references while printing a list to the screen. Some (like CLISP) are not! They'll go into an infinite loop trying to print such beasts. Try it out on osf1 for some fun as well.
> (setf *x* '(a b c d e)) (A B C D E) > (setf *y* (rest *x*)) (B C D E) > (setf (rest *y*) '(1 2 3)) (1 2 3) > *y* (B 1 2 3) > *x* (A B 1 2 3) [ hey, wait a minute! ] > (setf *x* '(a)) (A) > (setf (cdr *x*) *x*) (A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A ... [ ad naseum ]
There are a number of destructive versions of list functions. Just as in applying the destructive sequence functions to list, the same warning applies: destructive functions are faster and use much less memory, but because lists are strung together like beads of pearls, make sure you know what you're doing if you choose to use them! Here are two common ones:
nconc is the destructive version of append.
nbutlast is the destructive version of butlast.
> (setf *x* '(a b c d e)) (A B C D E) > (setf *y* '(1 2 3 4 5)) (1 2 3 4 5) > (nconc *x* *y*) (A B C D E 1 2 3 4 5) > *x* (A B C D E 1 2 3 4 5) [ what the ... ] > (nbutlast 4 *x*) (A B C D E 1) > *x* (A B C D E 1) [ it keeps modifying *x*! ]
Predicates and Types
Lisp has a number of predicates to compare equality. Here are some type-specific ones.
(= num1 num2) compares two numbers to see if they are equal. 2.0 and 2 are considered =. Also, -0 and 0 are =.
(char= char1 char2) compares two characters. (can you guess what char>, char<=, etc. do?)
(char-equal char1 char2) compares two characters in a case-insensitive way.
(string= str1 str2) compares two strings.
(string-equal str1 str2) compares two strings in a case-insensitive way.
There are also general equality predicates. These predicates vary in strength. Here are some loose descriptions.
(eq obj1 obj2) is true if obj1 and obj2 are the exact same thing in memory. Symbols and same-type numbers are the same thing: (eq 'a 'a) is true for example. But complex objects made separately aren't the same thing: (eq '(1 2 3) '(1 2 3)) is false. Neither are integers and floats eq with one another: (eq 0 0.0) is false. eq is fast (it's a pointer comparison).
(eql obj1 obj2) is is like eq but also allows integers and floats to be the same (as in (eq 0 0.0) is true). eql is the default comparator for most stuff.
(equal obj1 obj2) says two objects are equal if they are eql or if they "look equal" and are lists, strings and pathnames, or bit-vectors.
(equalp obj1 obj2) says two objects are equal if they look equal. equalp compares nearly every kind of Lisp thing, including all sorts of numbers, symbols, characters, arrays, strings, lists, hash tables, structures, files, you name it. equalp is the slowest comparator predicate, but you will generally find it to be the most useful.
Some numbers should be = but may not be due to numeric precision.
> (eql '(a b) '(a b)) NIL > (equalp '(a b) '(a b)) T > (= 0.25 1/4) T > (eq (setf *q* '(a b)) *q*) [ remember the function rule ] T > (string-equal "hello" "HelLo") T > (= 1/5 .2) NIL [ what the ... ? ]
Lisp also has predicates to determine the type of objects. You've already seen some such predicates: atom, null, listp.
(numberp obj) is true if obj is a number. There are a number of useful numerical predicates as well: oddp is true if the number is odd (see also evenp). zerop is true if the number is zero. plusp is true if the number is > 0. Etc.
characterp is true if obj is a character. There are a number of subpredicates, such as alphanumericp which is true if the character is a letter or a number.
symbolp is true if it's a symbol. stringp is true if it's a string. arrayp is true if it's an array. vectorp and simple-vector-p are...well you get the idea. There's a lot of this stuff.
> (numberp 'a) NIL > (stringp "hello") T
Lisp has a general type-determination predicate called typep. It looks like this:
(typep expression type)
A type is (usually but not always) a symbol representing the type (you have to quote it -- it's evaluated). Example types include number, list, simple-vector, string, etc.
Types are organized into a hierarchy: thus types can have subtypes (simple-vector is a subtype of vector, which is a subtype of array, for example). The root type is t. The typep function returns true if the expression has the type that as its base type or as a supertype.
Numeric types in particular have quite a lot of subtypes, such as fixnum (small integers), bignum (massive integers), float, double-float, rational, real, complex, etc.
> (typep 'a 'symbol) T > (typep "hello" 'string) T > (typep 23409812342341234134123434234 'bignum) T > (typep 23409812342341234134123434234 'rational) T > (typep 1/9 'rational) T > (typep 1/9 'list) NIL > (typep 1/9 'foo) *** - TYPEP: invalid type specification FOO
You can get the type of any expression with (type-of expr)
> (type-of 'float) SYMBOL > (type-of 1/3) RATIO > (type-of -2) FIXNUM > (type-of "hello") (SIMPLE-BASE-STRING 5) [ types can be lists starting with a symbol ] > (type-of (make-array '(3 3))) (SIMPLE-ARRAY T (3 3)) > (type-of nil) NULL
Many objects may be coerced into another type, using the coerce function:
(coerce expression type)
Vectors and lists may be coerced into one another.
Strings may be coerced into other sequences, and lists or vectors of characters can be coerced into strings.
Integers may be coerced into floats. To convert a float or other rational into an integer, use one of the functions floor, round, truncate (round towards zero), or ceiling.
> (coerce 4 'float) 4.0 > (coerce "hello world" 'list) (#\h #\e #\l #\l #\o #\Space #\w #\o #\r #\l #\d) > (coerce '(#\h #\e #\l #\l #\o #\Space #\w #\o #\r #\l #\d) 'string) "hello world" > (floor -4.3) -5 > (coerce '(a b c) 'simple-vector) #(A B C) > (coerce '(a b c) 'string) *** - SYSTEM::STORE: A does not fit into "", bad type
While we're on the subject of the four rounding functions (floor, round, truncate, ceiling), these are how you do integer division. Each function takes an optional argument, and divides the first argument by the second, then returns the appropriate rounding as an integer.
If you're used to C++ or Java's integer division, probably the most obvious choice is truncate.
Lisp functions can actually return more than one item. For example, integer division functions return both the divided value and the remainder. Both are printed to the screen. The primary return value (in this case, the divided value) is returned as normal. To access the "alternate" return value (in this case, the remainder), you need to use a macro such as multiple-value-bind or multiple-value-list (among others).
> (floor 9 4) 2; [ the primary return value ] 1 [ the alternative return value ] > (floor -9 4) -3 ; 3 > (truncate -9 4) -2 ; -1 > (* 4 (truncate -9 4)) -8 [ 4 mulplied against the primary return value ] > (multiple-value-list (truncate -9 4)) (-2 -1) > (multiple-value-bind (x y) (truncate -9 4) (* x y)) 2
Hash tables are created with make-hash-table. You can hash with anything as a key. Hash tables by default use eql as a comparison predicate. This is almost always the wrong predicate to use: you usually would want to use equal or equalp. To do this for example, you type:
(make-hash-table :test #'equalp)
Elements are accessed with gethash. If the element doesn't exist, nil is returned. An alternative return value indicates whether or not the element exists (returning T or NIL). If you stored nil as the value, then we have a problem! Instead of having to look up the alternate return value, you can supply an optional return value (instead of nil) to return if the slot really is empty.
(gethash key hashtable &optional return-if-empty)
Use setf to set hashed values.
(setf (gethash key hashtable) value)
Remove elements with remhash.
(remhash key hashtable)
Although it's not very efficient, you can map over a hashtable with maphash.
(maphash function hashtable)
function must take two arguments (the key and the value).
> (setf *hash* (make-hash-table :test #'equalp)) #S(HASH-TABLE EQUALP) > (setf (gethash "hello" *hash*) '(a b c)) (A B C) > (setf (gethash 2 *hash*) 1/2) 1/2 > (setf (gethash 2.0 *hash*) 9.2) [ 2.0 is equalp to 2 ] 9.2 > (gethash 2 *hash*) 9.2 [ because we're using equalp as a test ] T [ T because the slot exists in the hashtable ] > (setf (gethash #\a *hash*) nil) [ store NIL as the value ] NIL > (gethash #\b *hash*) NIL; [ No such key #\b in *hash* ] NIL > (gethash #\a *hash*) NIL; [ uh... wait a minute... -- NIL is returned! ] T > (gethash #\b *hash* 'my-empty-symbol) MY-EMPTY-SYMBOL ; NIL > (gethash #\a *hash* 'my-empty-symbol) NIL; [ that's better! ] T > (maphash #'(lambda (key val) (print key)) *hash*) 2 "hello" NIL
Printing and Reading
(tepri) prints a linefeed.
(print obj) of course prints a linefeed followed by obj (in a computer readable fashion). Unlike Java's System.println("foo") or C's printf("foo\n"), in Lisp it's traditional to print the newline first.
(prin1 obj) prints obj (in a computer readable fashion) -- no prior linefeed.
(princ obj) prints obj in a human readable fashion -- no prior linefeed. Strings are printed without "quotes", for example. Such printed elements aren't guaranteed to be readable back into the intepreter.
> (progn (terpri) (terpri) (terpri) (print 'hello)) HELLO HELLO > (progn (prin1 2) (prin1 '(a b c)) (prin1 "hello")) 2(A B C)"hello" "hello" > (progn (princ 2) (princ '(a b c)) (princ "hello")) 2(A B C)hello "hello"
(prin1-to-string obj) is like prin1, but the output is into a string.
(princ-to-string obj) is like princ, but the output is into a string.
> (prin1-to-string 4.324) "4.324" > (prin1-to-string "hello world") "\"hello world\"" > (princ-to-string "hello world") "hello world" > (prin1-to-string '(a b "hello" c)) "(A B \"hello\" C)" > (princ-to-string '(a b "hello" c)) "(A B hello C)"
(read) reads in an expression from the command line.
read is a complete Lisp parser: it will read any expression.
(read-from-string string) reads in an expression from a string, and returns the expression plus an integer indicating at what point reading was completed.
(y-or-no-p) waits for the user to type in a yes or a no somehow, then returns it. The way the question is presented the user (graphical interface, printed on screen, etc.) is up to the Lisp system. y-or-no-p is a predicate.
> (read) [ Lisp waits for you to type an expression ] '(a b c d) (A B C D) > (read-from-string "'(a b c d)") '(A B C D) ; [ Or equivalently (QUOTE (A B C D)) ] 10 [ Reading the expression finished before the tenth character ] > (y-or-n-p) [ clisp waits for you to type y or n ] TRUE [ I tried to type in TRUE ] Please answer with y or n : y [ oh, okay! ] T
format is a much more sophisticated printing facility. It is somewhat similar to C's printf command plus formating string. But format's formatting string is much more capable. Generally, format looks like:
(format print-to-where format-string obj1 obj2 ...)
print-to-where can be t (print to the screen) or nil (print to a string).
Formatting sequences begin with a tilde (~). The simplest sequences include: ~a (princ an element); ~% (print a linefeed); ~s (prin1 an element). Much more complex formatting includes very complex numerical printing, adding spaces and buffers, printing through lists, even printing in roman numerals! format has its own little programming language. It's astounding what format can do.
> (format t "~%My name is ~a and my ID is ~a" "Sean" 1231) My name is Sean and my ID is 1231 NIL > (format nil "~%~%~%~a~a~s ~a" '(a b c) #(1 2 3) "yo" 'whatever) " (A B C)#(1 2 3)\"yo\" WHATEVER" > (format t "~% ~a ~R ~:R ~@R ~:@R ~$ ~E" 4 4 4 4 4 4 4) 4 four fourth IV IIII 4.00 4.0E+0 [ hee hee hee! ] NIL
More Control Structures
(when test expr1 expr2 ...) evaluates the expressions (and returns the last) only if test is true, else it returns nil.
(unless test expr1 expr2 ...) evaluates the expressions (and returns the last) only if test is nil, else it returns nil.
(case test-object case1 case2 ... ) goes through the cases one by one and returns the one which "matches" the test-object. A case looks like this:
(obj expr1 expr2 ... )
If obj (not evaluated, so you shouldn't quote it) is an object which is eql to test-object, or is a list in which test-object appears, then the case "matches" test-object. In this case, the expressions are evaluated left-to-right, and the last one is returned. obj can also be t, which matches anything. This is the "default" case.
If no case matches, then case returns nil.
case is a lot like the Java/C++ switch statement. There are other versions: ecase, ccase.
> (unless (y-or-n-p) (print "you picked no!") (print "good for you!")) n "you picked no!" "good for you!" "good for you!" > (defun type-discriminator (obj) "Prints out a guess at the type" (let ((typ (type-of obj))) (when (consp typ) (setf typ (first typ))) (case typ ((fixnum rational ratio complex real bignum) (print "a number perhaps?")) ((simple-vector vector string list) (print "some kind of sequence?")) (hash-table (print "hey, a hash table...")) (nil (print "it's nil!")) (t (print "beats me what this thing is. It says:") (print (type-of obj)))))) TYPE-DISCRIMINATOR > (type-discriminator 42) "a number perhaps?" "a number perhaps?" > (type-discriminator "hello") "beats me what this thing is. It says:" (SIMPLE-BASE-STRING 5) (SIMPLE-BASE-STRING 5)
cond is a powerful generalization of case. It takes the form:
(cond (test1 expr expr ... ) (test2 expr expr ... ) (test3 expr expr ... ) ... )
cond works like this. First, test1 is evaluated. If this is true, the following expressions are evaluated and the last one is returned. If not, then test2 is evaluated. If this is true, its following expressions are evaluated and the last one is true. And so on. If no test evaluates to true, then nil is returned.
[ Previously, type-discriminator didn't work for string. let's get it working right. ] > (defun type-discriminator (obj) "Prints out a guess at the type" (cond ((find-if #'(lambda (x) (typep obj x)) '(fixnum rational ratio complex real bignum)) (print "a number perhaps?")) ((find-if #'(lambda (x) (typep obj x)) '(simple-vector vector string list)) (print "some kind of sequence?")) ((typep obj 'hash-table) (print "hey, a hash table...")) ((typep obj null) (print "it's nil!")) (t (print "beats me what this thing is. It says:") (print (type-of obj))))) TYPE-DISCRIMINATOR > (type-discriminator "hello") "some kind of sequence?" "some kind of sequence?"
do is a general iterator. It takes the form:
(do (initial-variable-declarations) (test res-expr1 res-expr2 ... ) expr1 expr2 ... )
do works like this. First, local variables are declared in a way somwhat similarly to let (we'll get to that). Then test is evaluated. If it is true, then the res-expr's are evaluated and the last one is returned (if there are none, then nil is returned).
If test returned false, then expr's in the body are evaluated. Then do iterates again, starting with trying test again. And so on.
A variable declaration is either a variable name (a symbol), just as in let, or it is a list of the form (var optional-init optional-update) The optional-init expression initializes the variable (else it's nil). The optional-update expression specifies the new value of var each iteration. optional-update is evaluated in the context of the variables of the previous iteration.
[ generate some random numbers ] > (defun generate (num) (do ((y 0 (1+ y)) (x 234567 (mod (* x 16807) 2147483647))) ((>= y num) "the end!") (print x))) GENERATE > (generate 20) 234567 "the end!"
loop is a very powerful, complex iteration macro which can do nearly anything. Literally. It has its own language built into it. loop is one of the few things in Lisp more complex than format.
loop has an idiosyncratic syntax that is very un-lisp-like. It is also so complex that few people understand it, and it is not recommended for use. We will not discuss loop except to mention that its very simplest form: (loop expressions ... ) makes a very nice infinite loop.
> (loop (print 'hello) (print 'yo)) HELLO YO HELLO YO HELLO YO HELLO YO [ ... ad nauseum until you press Control-C ]
A block is a sequence of expressions. Blocks appear in lots of control structures, such as let, all iterators (do, dotimes, dolist, loop, etc.), many conditional statements (cond, case, when, etc.), progn, etc.
Blocks have labels (names). In control structures, the implicit blocks are all named nil.
Functions created with defun have an implicit block whose label is the same name as the function. Functions created with lambda have an implicit block whose label is nil.
(return-from label optional-value) will exit prematurely from a block whose label is label (not evaluated -- don't quote it). This is somewhat like Java/C++'s break statement. The return value of the block is optional-value (or nil if no value provided).
Because so many blocks are named nil, the simpler (return optional-value) is the same thing as (return-from nil optional-value)
Use return and return-from sparingly. They should be rare.
> (dotimes (x 100) (print x) (if (> x 10) (return 'hello))) 0 1 2 3 4 5 6 7 8 9 10 11 HELLO > (defun differents (list &key (test #'eql)) "Returns the first different pair in list" (dolist (x list) (dolist (y list) (unless (funcall test x y) (return-from differents (list x y)))))) DIFFERENTS > (differents '(a a a b c d)) (A B) > (differents '(a a a a a a)) NIL
Another way to escape is with catch and throw. catch looks like this:
(catch catch-symbol expressions ... )
throw looks like:
(throw catch-symbol return-value)
Normally, catch works just like progn. But if there is a throw statement inside the catch whose catch-symbol matches the catch's, then we prematurely drop out of the catch and the catch returns the return value of the throw.
This works even if the throw appears in a subfunction called inside the catch.
In C++ such a thing is done with longjump. In Java such a thing is done with an exception.
[ another way to do the differents function ] > (defun differents (list &key (test #'eql)) "Returns the first different pair in list" (catch 'my-return-value (dolist (x list) (dolist (y list) (unless (funcall test x y) (throw 'my-return-value (list x y))))))) DIFFERENTS > (differents '(a a a b c d)) (A B) > (differents '(a a a a a a)) NIL
Writing Lisp in Lisp
Lisp has a built-in interpreter. It is called eval, and looks like this:
eval takes data and submits it to the Lisp interpreter to be executed.
The data submitted to the interpeter is not evaluated in the context of any current local variables.
eval is powerful. You can assemble lists and then have them executed as code. Thus eval allows you to make lisp programs which generate lisp code on-the-fly. C++ and Java can only do this in truly evil ways (like writing machine code to an array, then casting it into a function, yikes!).
> (list '+ 4 7 9) (+ 4 7 9) > (eval (list '+ 4 7 9)) 20
Lisp has an interpeter eval, a full-featured printer print, and a full-featured parser read. Using these tools, we can create our own Lisp command line!
> (loop (format t "~%my-lisp --> ") (print (eval (read)))) my-lisp --> (dotimes (x 10) (print 'hi)) HI HI HI HI HI HI HI HI HI HI NIL my-lisp -->
(break) signals an error, just as if the user pressed Control-C.
You can continue from a break.
> (defun foo (x) (print (+ x 3)) (break) (print (+ x 4))) FOO > (foo 7) 10 ** - Continuable Error Break If you continue (by typing 'continue'): Return from BREAK loop 2. Break > continue [in clisp, anyway] 11 11
(trace function-symbol) turns on tracing of a function. function-symbol is not evaluated (don't quote it or sharp-quote it).
When a trace function is entered, the function and its arguments are printed to the screen. When the trace function exits, its return value is printed to the screen.
You can trace multiple functions at the same time.
You turn off tracing of a function with (untrace function-symbol)
> (defun factorial (n) (if (<= n 0) 1 (* n (factorial (- n 1))))) FACTORIAL > (trace factorial) (FACTORIAL) > (factorial 15) 1. Trace: (FACTORIAL '15) 2. Trace: (FACTORIAL '14) 3. Trace: (FACTORIAL '13) 4. Trace: (FACTORIAL '12) 5. Trace: (FACTORIAL '11) 6. Trace: (FACTORIAL '10) 7. Trace: (FACTORIAL '9) 8. Trace: (FACTORIAL '8) 9. Trace: (FACTORIAL '7) 10. Trace: (FACTORIAL '6) 11. Trace: (FACTORIAL '5) 12. Trace: (FACTORIAL '4) 13. Trace: (FACTORIAL '3) 14. Trace: (FACTORIAL '2) 15. Trace: (FACTORIAL '1) 16. Trace: (FACTORIAL '0) 16. Trace: FACTORIAL ==> 1 15. Trace: FACTORIAL ==> 1 14. Trace: FACTORIAL ==> 2 13. Trace: FACTORIAL ==> 6 12. Trace: FACTORIAL ==> 24 11. Trace: FACTORIAL ==> 120 10. Trace: FACTORIAL ==> 720 9. Trace: FACTORIAL ==> 5040 8. Trace: FACTORIAL ==> 40320 7. Trace: FACTORIAL ==> 362880 6. Trace: FACTORIAL ==> 5. Trace: FACTORIAL ==> 4. Trace: FACTORIAL ==> 3. Trace: FACTORIAL ==> 2. Trace: FACTORIAL ==> 1. Trace: FACTORIAL ==> > (untrace factorial) (FACTORIAL) > (factorial 15)
You can step through an expression's evaluation, just as in a debugger, using (step expression). The features available within the step environment are implementation-dependent.
In clisp, the step function lets you interactively type, among other things, :s (to step into an expression), :n (to complete the evaluation of the expression and step out), and :a (to abort stepping)
> (defun factorial (n) (if (<= n 0) 1 (* n (factorial (- n 1))))) FACTORIAL > (step (factorial 4)) (step (factorial 4)) step 1 --> (FACTORIAL 4) Step 1 > :s step 2 --> 4 Step 2 > :s step 2 ==> value: 4 step 2 --> (IF (<= N 0) 1 (* N (FACTORIAL #))) Step 2 > :s step 3 --> (<= N 0) Step 3 > :n step 3 ==> value: NIL step 3 --> (* N (FACTORIAL (- N 1))) Step 3 > :s step 4 --> N Step 4 > :s step 4 ==> value: 4 step 4 --> (FACTORIAL (- N 1)) Step 4 > :s step 5 --> (- N 1) Step 5 > :n step 5 ==> value: 3 step 5 --> (IF (<= N 0) 1 (* N (FACTORIAL #))) Step 5 > :n step 5 ==> value: 6 step 4 ==> value: 6 step 3 ==> value: 24 step 2 ==> value: 24 step 1 ==> value: 24 24
The apropos function can be used to find all the defined symbols in the system which match a given string. Ordinarily, apropos will return everything, including private system symbols. That's not what you'd want. But the following will do the trick:
(apropos matching-string 'cl-user)
NOTE: A bug in CMUCL (CMU Common Lisp) means that it doesn't know to join the 'cl and 'cl-user packages together into just 'cl-user when responding to apropos. So if you're using CMUCL, you need to do something like:
(defun my-apropos (string) (apropos string 'cl) (apropos string 'cl-user))
...then you can call (my-apropos "compile") and you'll get the right stuff. Don't bother with this hack on CLISP, LispWorks, or other correctly-working Lisps with regard to apropos.
[ in clisp ] > (apropos "compile" 'cl-user) *COMPILE-FILE-PATHNAME* variable *COMPILE-FILE-TRUENAME* variable *COMPILE-PRINT* variable *COMPILE-VERBOSE* variable *COMPILE-WARNINGS* variable *COMPILED-FILE-TYPES* variable COMPILE function COMPILE-FILE function COMPILE-FILE-PATHNAME function COMPILED-FUNCTION type COMPILED-FUNCTION-P function COMPILER-LET special operator COMPILER-MACRO COMPILER-MACRO-FUNCTION function DEFINE-COMPILER-MACRO macro UNCOMPILE function
You can disassemble a function with (disassemble function-pointer)
You can use disassemble to assess the quality of your function in terms of machine code instructions. Compiled and interpreted functions may or may not appear to disassemble differently. Disassembly is implementation-dependent and processor-dependent.
[ in clisp ] > (defun factorial (n) (if (<= n 0) 1 (* n (factorial (- n 1))))) FACTORIAL > (disassemble 'factorial) Disassembly of function FACTORIAL (CONST 0) = 0 (CONST 1) = 1 1 required arguments 0 optional arguments No rest parameter No keyword parameters 0 L0 0 (LOAD&PUSH 1) 1 (CONST&PUSH 0) ; 0 2 (CALLSR&JMPIF 1 50 L16) ; <= 6 (LOAD&PUSH 1) 7 (LOAD&DEC&PUSH 2) 9 (JSR&PUSH L0) 11 (CALLSR 2 56) ; * 14 (SKIP&RET 2) 16 L16 16 (CONST 1) ; 1 17 (SKIP&RET 2) #<COMPILED-CLOSURE FACTORIAL> [ dunno why clisp says it's compiled ]