CSE 473 Autumn 1998, Copyright, S. Tanimoto, Univ. of Washington 
Introduction to Artificial Intelligence (Oct. 2, 1998)

"Interacting With Lisp"


Getting Started

In Windows...
  Download Allegro Common Lisp for Windows, Lite version.
  or use XLISP-STAT.

In Linux
  Download the free version of ACL5.0 at www.franz.com

Under Unix
  Run ACL, possibly within Gnu Emacs.

On the Mac
  Use XLISP-STAT or Macintosh Common Lisp
 


 

The READ-EVAL-PRINT Loop


 At the "top level" of interpretation, a Lisp "listener"
   waits for the user to type in an S-expression on one
   or more lines.
      Parentheses must balance.
      Double quotes (for strings) must balance.

Next, the interpreter tries to evaluate the expression.

Finally, the value is printed out.

Then the cycle repeats


 
 

The Evaluator

If the expression is a constant of any type, including
      number, keyword, array, or structure, it evaluates to itself.

If the expression is a symbol, then its SYMBOL-VALUE,
      if any, is returned.  If it has not value, then the interpreter
      reports that it is an Unbound variable.

If the expression is a list, then the interpreter tries to use
     the first element of the list as an operator -- either a
     function, a special form, or a macro.

       If the first element is the name of a function,
          then the remaining elements of the list are evaluated,
          and the function is applied to the resulting values.

       If the first element is the name of a special form,
          then the arguments are processed in some
          specific way, and some value is returned.

       If the first element is the name of a macro, then
          the macro expander function for that macro is
          applied to a list of the remaining elements,
          and the expression that results is evaluated.
 


 

Numeric Expressions

  Arithmetic operations use the function syntax,
     operator first, argument following.
>   (+  3  5)
8

  Many arithmetic functions can take an arbitrary
     number of arguments.
>   (+  1  2  3)
6
>   (+  1)
1
>   (+)
0
>   (- 10  1)
9
>   (- 10  1  2)
7
>   (/  10  2)
5
>   (/  10  2  3)
5/3

  A ratio is not only a mathematical concept but a
Common Lisp-supported type of number.
>  (+  5/3  1/3)
2
>  (+  5/3  1/4)
23/12

  Floating point numbers are an absorbing type.
> (+ 1/2   0.25)
0.75
> 1/2.
Error unbound variable '1/2.'

  A Bignum is an integer stored in a format that
allows arbitrarily large magnitudes.

> (defun fact (n)
      (if (= n 0) 1
           (* n (fact (1-  n))) )) )
FACT
>  (fact  5)
120
>  (fact  20)
2432902008176640000
>  (+  *  0.0)
2.432902e+18
 
 
 
 
 


 

Lists

> '(a  b  c)
(A B C)
> '(setq  x  *)
(A B C)
>  (first  x)
A
>  (rest  x)
(B C)
>  x
(A B C)

Lisp lists are typically represented internally as linked lists.
FIRST accesses the element "here" at the head of the list.
REST accesses what it pointed to by outgoing pointer from
 the first cell.

>  (cons 'a '(b c))
(A B C)
>  (cons 'a '() )
(A)
>  (cons 'a  'b)
(A . B)

A dotted pair represents a single "cons" or memory cell
that contains a FIRST and REST component.

> ( )
NIL
> nil
NIL
> (=  1  2)
NIL
> (rest '(a) )
NIL

> (null  '() )
T
> (null  '(a) )
NIL

Lists can be nested to any level
> '(a  b  (c  d  (e  f)  g) )
(A B (C D (E F) G))
 
 
 


 
 

Symbols and Their Values

>  (setq  x  1 )
1
>  x
1
>  (+ x 3)
4
>  x
1
>  (incf  x)
2
>  x
2
>  y
Error unbound variable Y
> (setq  y  x)
2
>  y
2
>  (let  ((x 39))
      (print (+ x 5))
     )
44
44
>  x
2
>  (let ((x  39))
      (print (+ (symbol-value  x)  5))
     )
7
7
> x
2
 

>  (setq  x  1)

1
 
 


 
 

Functions

> (setq lst '(a  b  c))
(A B C)
>  (first  lst)
A
> (cons (first lst) (rest lst))
(A B C)
>  (list  'a  'b  'c)
(A B C)
>  (list  lst)
((A B C))
> (setq  fun  #'first)
#<function .....>
> (apply fun (list lst))
A
> (setq plus #'+)
#<function ....>
>  (apply plus '(1  2  3  4  5))
15

Here's a function definition with a simple conditional:
>  (defun  big-or-small  (n)  (if (> n 99) 'big 'small) )
BIG-OR-SMALL
>  (big-or-small 100)
BIG
>  (big-or-small 99)
SMALL
 
 
 
 


 
 

Loading Files of S-expressions

Suppose file FACT.LSP  contains
(defun fact (n)
  (if (= n 0) 1)
      (* n (fact (1- n))) ) )
(fact 5)

Then we can bring it into a Lisp session...
>  (load "FACT.LSP")
 
 


 
 

Class-time Lab Exercises

1. Write a function DOUBLE that returns 2 times a number.
 

2. Write a function TO-ORDINAL that takes as its argument
an integer in the range 1 to 3 and outputs FIRST, SECOND,
or THIRD.  For example it should behave this way:
> (to-ordinal 2)
SECOND
 

3. Write a recursive function that sums the first n odd numbers.
 It should have the behavior...
> (sum-odd-numbers 5)
9
 
 
 

Last modified: October 2, 1998

Steve Tanimoto

tanimoto@cs.washington.edu