Intro to LISP Walkthru

MCS 380/590 Intro AI, Fall 2000 Chris Cera Luiza Helena da Silva

This document is available at http://www.mcs.drexel.edu/~uccera/class/CS380/lisp.tutorial.html


LINKS


Interpreters

Allegro Common LISP http://www.franz.com

also on Queen machines in MCS lab. This is text terminal based.

Harlequin LISPworks http://www.harlequin.com

www.xanalys.com

gcl (GNU Common LISP) http://www.gnu.org/software
CLISP (ANSI Common LISP) http://www.gnu.org/software

Some examples in this introduction are done using Harlequin LISPworks for Linux, and other examples are done using Allegro Common Lisp 5 for Linux.


LISP

List Processing ~ Lots of Irritating Single Parentheses

Dialects: Everything we will cover is part of ANSI Common LISP.


A prompt or arrow is the symbol that tells you the listener is waiting for input.


CL-USER 0 >
                                                                 


Debugger

Whenever you execute an illegal instruction the debugger halts the interpreter and jumps into the debugger.

A debugger is not part of Common LISP, so may not work on all implementations. Another 'exit' command is possible among different interpreters. That really goes for all commands being implementation specific since it is not covered in any standard.


Exiting

Almost all implementations should support the exit function to gracefully exit the interpreter.

USER(19): (exit)
; Exiting Lisp

Evaluation


CL-USER 1 > ;; (8 + 3) * (4 + (9 * 2) )


CL-USER 2 > (* (+ 8 3) (+ 4 (* 9 2)))    
242

  • LISP evaluates everything, and everything has a return value. Atoms evaluate to themselves. So everytime they are evaluated, they simply return themselves.

    
    CL-USER 3 > 8
    8
    
    CL-USER 4 > 3
    3
    

  • LISP has the ability to postpone evaluation until the next evaluation. This is useful whenver you want to build up some kind of a state using a list without having the first argument be taken literally as a function call.

    
    CL-USER 44 > (a b c)
    
    Error: The variable B is unbound.
      1 (continue) Try evaluating B again.
      2 Return the value of :B instead.
      3 Specify a value to use this time instead of evaluating B.
      4 Specify a value to set B to.
      5 (abort) Return to level 0.
      6 Return to top loop level 0.
    
    Type :b for backtrace, :c <option number> to proceed,  or :? for other options
    
    CL-USER 45 : 1 > :c 6
    
    CL-USER 46 > (quote (a b c) )
    (A B C)
    

    CL-USER 47 > '(a b c) (A B C)

  • Another way to pass in a list is using:

    
    CL-USER 48 > (list 'a)
    (A)
    CL-USER 49 > (list 'a '(b c) 'd)
    (A (B C) D)
    


    Operators

    
    CL-USER 38 > (1+ 10)
    11
    

    Symbols


    Predicates


    Essential List Construction Functions


    Creating local variables

    In lisp, you can create local variables using the let function. This makes use of a variable as efficient as possible.

    ; b^2 - 4 a c
    
    USER(1): (defun disc (a b c)
        (let ((d))
            (setq d (- (* b b) (* 4 a c)))))
    DISC
    USER(2): (disc 1 3 4)
    -7
        


    Prompting for user input

    Prompting for user input involves using the read function. This automatically stops execution and prompts for user input. Similiar to reading from stdin in a C program. This also gives a great practical use of the let function, since we only need one everytime we have input to be read in.

    
    USER(12): (defun start_sqrt ()
        (loop
            (print 'number>)
            (let ((in (read)))
                (if (equal in 'end) (return nil))
                (prin1 (sqrt in)))))
    START_SQRT
    USER(13): (start_sqrt)
    
    NUMBER> 23
     4.7958317
    NUMBER> 45
     6.708204
    NUMBER> 47
     6.8556547
    NUMBER> 49
     7.0
    NUMBER> end
     NIL
        


    Structured vs. Unstructured Iteration

    Structured iteration means iteration that does not include any explicit goto's in the code. Where unstructured iteration requires explicit branching statements to effect the flow of control.


    Cons Cells

    To draw illustrate the internal representations of cons cells for the following function calls:

    
    (setf x '((a b) c d))
    (setf y '(first (cdr (cdr x))))
    

    x-------->[ | ]------------------->[ | ]---------------->[ |\]
               |                        |                     |
               |                        C      /------------->D
              [ | ]->[ |\]                     |
               |      |                        |
               A      B                        |
    y------------------------------------------/
    

    Functions and Lambda Expressions


    Profiling

    Profiling a function call can be useful when trying to evaluate the performance of a particular function. The time function will print usefull information that you may want to gather:


    Loading the AIMA Code Repository


    Functions and Examples

    see lisp.examples.lsp


    Bibliography