LISP
John McCarthy MIT 1960's
No standard -- Franz LISP (1980)
Common LISP
LISP -- functional programming language
expressions formed of function calls are basic unit of programs
a.programs and data are equivalent
b.basic control structure is recursion
c.linked list is basic data structure
d.introduced concept of garbage collection
LISP functions are expressions
operators are functions which return a value
data are restricted to:
elementary data types:
literal atoms (symbols)
numeric atoms (numbers)
basic data structures
lists
property lists
control structures
Cambridge Polish form
(prefix notation)
may contain conditional branching
LISP
represents partial recursive functions for a class of symbolic
expressions (s-expressions)
s-expressions are binary tree representations built up from atoms
(consider atoms like variables -- a series of letters and numbers
where first character is a letter) They are atoms, atoms in dot
notation, or s-expressions in dot notation.
nil the symbol () and is a type of s-expression
list something in parentheses (A B D)
(A)
dotted pair (A.B)
Elements of a list are atoms, a list of atoms, or a list of lists.
Dot notation is sufficient for representing all list structures
LISP interpreter provides functions which operates on s-expressions
car -- contents of address register
cdr -- contents of decrement register
cons -- construct
LISP information binding function is LAMBDA, obtained from Church's lambda calculus
Graph notation
(A.B) A | B ((B.C).A) | A
((A.B).A) | AB | C
A | B(A.nil) A |\
((A.D).(C.B)) |(A.(B.nil))
A | D C | B A |
B |/
Rules for transforming lists to dotted pairs
1. The leftmost element is left part of dotted pair.
2. If the first element in the list is the last element, then
(ATOM) = (ATOM.nil)
3. If not, the right part is a list formed by removing the first element
(A B C) = (A.(B.(C))) = (A.(B.(C.nil)))
(A(B C)D) = (A.((B C)D)) =
(A.(B C).(D)))= (A.((B.(C.nil)).(D.nil)))
cons scans the construct of the two arguments and forms a dotted
pair of s-expressions, with the first half of the pair being the
left part of the expression, and the second half being the right
part of the expression
(cons A (B) ) = (A B)
(cons (A) (B)) = ((A) B)
(cons A (B C D)) = (A B C D)
car extracts a subexpression of a non-atomic S-expression. If the
s-expresssion is in dot notation, the sub expression is the left
part of the s-expression; if the s-expression is in list notation
then the subexpression is the first element of the s-expression.
(The car of an atom is undefined)
(car (M N)) = M
(car ((A B C) D E )) = (A B C)
cdr (pronouced coulder) extracts a sub-expression of a non-atom.
If the s-expression is in list notation, then the subexpression
consists of the s-expression with the first element removed
(cdr (M N)) = (M)
(cdr ((A B C) D E ) = (D E)
combinations:
(caddr ((A B) C D)) means
(car (cdr (cdr ((A B) C D)))
(caar ((A B) C D)) means
(car (car ((A B) C D)))
FUNCTIONS
1. atom (atom X) the value is T if X is an atom, nil otherwise.
Three kinds of atoms: alpha atom, real atom and literal atom.
(atom 1.23) = t
(atom nil) = t (atom L) = t if L atom
(atom '(A B C)) = nil (atom '|joe|) = t
2. quote (quote (A B C)) = (A B C)
allows LISP interpreter to distinguish between characters used as
variables and characters used as literals abbreviated to '
3. eq (eq 'A 'A) operates on two atoms
returns true if they are both the same, nil otherwise. Operates
only on atoms.
4. null (null L) where L is (A B C) = nil where L is () = t
5. setq (setq L '(A B C D)) assigns a variable a value. Stands
for set the first identifier to quote the second argument.
L = (A B C D)
Math functions
1. + (+ fn1..fnn) (+ 6 8) would give 14
(+ 8 7 3) would give 18
also -, / , *
2. < (< fn1 fn2) (< 8 2) would give nil
also =, >
Bindings and function definition
1. lambda binds formal parameters to actual parameters used in a
function's form
((lambda (list of variables) (form)) actuals))
((lambda (x y) (- y x)) 9 5) returns 4
((lambda (x y)(cons y x)) '(my dog)'|has|)
2. def allows the association of a name with a lambda function to
delay binding until called.
(def name (lambda (list of variables) (form))
(def Double (lambda (x) (+x x))
(Double 5 ) would return 10
3. defun is a short cut which includes lambda.
(defun name (list of variables) (form))
(defun Triple (x) (+x x x))
(Triple 4) would give 12
CONDITIONALS
a conditional expression is written as proposition expression
pairs
[P1 -> E1; P2 -> E2;ÉPn -> En]
where P is a boolean and E is any expression
implemented as
(cond (pair) (pair) (pair))
(cond ((atom X) X) (T (car X)))
(cond ((atom X) X) ((atom (car X)) (car X)) (T (cdr X)))
evaluate for X is (A B C)
((A B) C)
A
(defun append (L M)
(cond ((null L) M)
(T (cons (car l) (append (cdr L) M)) )))