LISP (LISt
Processor)
Program = Data
Data in Lisp are called s-expressions (symbolic-expressions).
Indivisible objects are called atoms.
s-expressions:
atoms:
identifiers
numerals
strings
lists:
stacks of atoms or stacks
nil is an atom and a list with no element, i.e., ( )
dotted-pairs:
(l . a) ;; where l is a list and a is an atom
Basic Functions:
Let expr be an s-expression.
(quote expr) = expr
(car expr)
(cdr expr)
(nth idx a-list)
(atom expr)
(equal expr1 expr2)
(cons expr1 expr2)
(list expr1 expr2 … exprn)
(append a-list b-list)
(cond (c1 a1) (c2 a2) …… (cn an)) ;; ci’s and ai’s are s-expressions
(if (expr) s1 s2) ;; s1 and s2 are s-expressions
(defun f (x1 x2 … xn) s-expr )
Logical connectives: and, or, not
T = not nil
False = nil
e.g. define a funny list:
(defun funnyList (list)
(list (list list) (list list ‘(a b))))
Save “run-time image” :
Recursion:
Tail Recursion (Linear recursion):
e.g.:
(myreverse nil) = nil
(myreverse ‘a) = nil
(myreverse ‘(a b c)) = (c b a)
(c b) a
(append ‘(c b) (list ‘a))
(append (myreverse (cdr inlist)) (list (car inlist)))
(defun myreverse (inlist)
(cond ((atom inlist) nil)
((null inlist) nil)
(t (append (myreverse (cdr inlist)) (list (car inlist) )))
))
Tree Recursion:
(defun myreverse (inlist)
(cond ((atom inlist) nil)
((null inlist) nil)
((atom (car inlist))
(append (myreverse (cdr inlist)) (list (car inlist)))) ;; first is an atom
(t (append (myreverse (cdr inlist)) (list (myreverse (car inlist)) )))
))