Lisp
Lisp
A brief overview
• Lisp stands for “LISt Process”
– Invented by John McCarthy (1958)
– Simple data structure (atoms and lists)
– Heavy use of recursion
– Prefix notation of expressions
– Interpretive language
• Why Lisp
– It is the most widely used AI programming language
– It is good for writing production software
– It is especially good for prototyping
– It is got lots of features other languages don’t
– You can write new programs and extend old programs
really, really quickly in Lisp
• Variations
– Frantz Lisp (80’s)
– Common Lisp (de facto industrial standard)
• Common Lisp at UMBC (from CMU)
– At both gl.umbc.edu and cs.umbc.edu
– command line: clisp
– Related links
– Lisp online:
– http://www.csee.umbc.edu/331/resources/lisp/onLisp/
– http://www.delorie.com/gnu/docs/emacs/cl_toc.html#SEC_Contents
– Lisp tutorial
1. Valid objects (S-expressions)
Atoms:
numbers: (real 1.0, integer 1)
symbols: a consecutive sequence of characters (no space)
e.g., a, x, price-of-beef.
two special symbols: T and NIL for logical true and false.
strings: a sequence of characters bounded by double quotes
e.g., "this is red".
(Note: LISP is case insensitive)
Lists: a list of atoms and/or lists, bounded by "(" and ")"
(a b c), (a (b c))
top elements of a list
example: top elements of list (a b c) are a, b, and c
top elements of list (a (b c)) are a and (b c)
nil: empty list (same as ()).
2. Function calls
• also a list
• use prefix notation: (function-name arg1 ... argn)
• returns function value for the given list of arguments
• functions are either provided by Lisp function library or
defined by the user.
• Examples:
>(+ 1 3 5)
9
>(/ 3 5)
3/5
>(/ 3.0 5)
0.59999999999999998
>(sqrt 4)
2
3. Evaluation of S-expression
1) Evaluate an atom.
numerical and string atoms are evaluated to themselves;
symbols evaluate to their values if they are assigned values,
return Error, otherwise;
The values of T and NIL are themselves.
2) Evaluate a list - evaluate every top element of the list as follows,
unless explicitly forbidden:
• the first element is always a function name;
evaluating it means to call the function body;
• each of the rest elements will then be evaluated, and their values
returned as the arguments for the function.
• Examples
>(+ (/ 3 5) 4) >(+ (sqrt 4) 4.0) >(sqrt x)
23/5 6.0 Error: variable X has
no value
3) To assign a value to a symbol (setq, set)
>(setq x 3.0) >x
3.0 3.0
• setq is a special form of function (with two arguments);
• the first argument is a symbol which will NOT be evaluated;
• the second argument is a S-expression, which will be evaluated;
• the value of the second argument is assigned to be the value of
the first argument
>(+ x z)
Error: X is not of a NUMBER ...
>(cddr L) >(caddr L)
>(cadr L) (C) C
B
; car of cdr of L >(cdddr L) >(cadddr L)
NIL NIL
tests if x is a atom
>(atom x) >(atom L)
T NIL
>(sum1 L1)
6
>(sum2 L1)
6
6. Other functions in LISP library
1) Predicates:zerop, plusp, evenp, oddp, integerp, floatp
2) Logical connector: and, or, not
5) Rounding: floor,ceiling, truncate, round
6) Others: max, min, abs, sqrt, 1+ (add 1), 1- (minus 1)
(exp number) (base-e exponential)
(expt Base-number Power-Number)
(log number & Optional base-number)
(isqrt number) Returns the greater integer less than equal to the
exact positive square-root of the number.
(signum number) Returns -1, zero, or 1 according if the number
is negative, zero, or positive.
7. Other features
1) Property lists:
Assigning/accessing properties (attribute-value pairs) of a symbol
To assign a property: (setf (get object attribute) value)
To obtain a property: (get object attribute)
Example:
>(setq L2 ‘(a b c))
Conditional
(if <test> body1 body2) ;do body1 if test is true,
; body2, otherwise
(when <test> body) ;do body when test is true
(unless <test> body) ;do body when test is false
6) Miscellaneous
%clisp ; enter Common Lisp of CMU (on gl.umbc.edu)
or cs.umbc.edu)
>(bye) or (quit)
or <ctrl>-D ; exit CLISP
(load "file-name") ; load in a file
(ed "file-name") ; enter vi editor
(compile-file "file-name") ; the compiled version is in file-name.o
; then load in file-name.o
(compile 'func-name) ; compile a particular function
(time (func-name arg1 ... argn))
; print real and run time for executing func-name
Summary
• Atoms and lists
• Functions and function calls
setq, setf, set, quote, eval,
math functions (+, -, *, /, max, min, exp, sqrt, …)
list operations: list, cons, car, cdr, length, nth, append, reverse
predicates (=, >, equal, eq, numberp, symbolp, …)
• Defining functions
(defun func_name (arg_list) func_body)
dolist, dotimes, cond, if, when, unless, mapcar
• Properties and associative lists: get, assoc
• Input/output: print, read, with-open-file, load