Professional Documents
Culture Documents
LISP
Why Lisp?
• Because it’s the most widely used AI
programming language
• Because it’s good for writing production
software (Graham article)
• Because it’s got lots of features other
languages don’t
• Because you can write new programs and
extend old programs really, really quickly in
Lisp
Great! How can I get started?
• On sunserver (CS) and gl machines, run
/usr/local/bin/clisp
• From http://clisp.cons.org you can
download CLISP for your own PC
(Windows or Linux)
• Great Lisp resource page:
http://www.apl.jhu.edu/~hall/lisp.html
Why all those parentheses?
• Surprisingly readable if you indent properly (use
built-in Lisp editor in emacs!)
• Makes prefix notation manageable
• An expression is an expression is an expression,
whether it’s inside another one or not
• (+ 1 2)
• (* (+ 1 2) 3)
• (list (* 3 5) ‘atom ‘(list inside a list)
(list 3 4) ‘(((very) (very) (very) (nested
list))))
Lisp basics
• Lisp syntax: parenthesized prefix notation
• Lisp interpreter: read-eval-print loop
• Nested evaluation
• Preventing evaluation (quote and other
special forms)
• Forcing evaluation (eval)
– Allows us to evaluate code contained in a Lisp
variable!
Basic Lisp types
• Numbers (integers, floating-point, complex)
– 27 -2 7.519
• Characters, strings (arrays of chars)
– #\x #\- #\B
– “This is a string!”
• Symbols, which have property lists
– ‘a ‘x ‘jon
• Lists (linked cells)
– Empty list: nil
– ‘(a b c) ‘(2 3 jon)
– cons structure has car (first) and cdr (rest)
Built-in functions
• For numbers
– + - * / incf decf
• A diversion: destructive functions
– (setf x 1)
– (setf y (+ x 1)) vs. (setf y (incf x))
• For lists
– car (first) cdr (rest) second third
fourth
– length nth
– cons append nconc list
– mapcar mapcan
– find remove remove-if
Built-in functions (cont’d)
• Printing: print, format
– (print “string”) print output
– (format …) formatted output