Lecture #11
Programming Languages
MISHRA 95
The value of an expression depends only on the values of its
subexpressions, if any.
---Slide 2---
Common Lisp
(Natural Language Processing, Computer Vision, Robot
Control Systems, Expert Systems, Neural Networks, Automatic Programming)
---Slide 3---
HISTORY
INTERLISP (PDP 10)
UCI-LISP (Univ. of Cal. at Irvine)
Standard Lisp (Univ. of Utah)
---Slide 4---
Common Lisp
---Slide 5---
Lisp Data & Functional Objects
Self-modifying Lisp programs.
Embedded languages in Lisp
Extensible control structures
---Slide 6---
Lisp Objects
,
,
A, EVAL, PI, T,
NIL 
(CONS 'A 'B)
(CONS '2 (CONS 'BAD 'NIL))
---Slide 7---
CONS Cells
---Slide 8---
LISTS
LIST
(LIST 'A (LIST 'B 'C) 'D) => (A (B C) D)
(LIST 'B NIL) => (B NIL)
(LIST) => NIL(An Atom not a
CONS cell)
---Slide 9---
Proper and Improper Lists
(A (B C) . D)
---Slide 10---
List Operations
CAR:
CDR:
CAR & CDR
NIL

(CAR '(A B C)) => A (CDR '(A B C)) => (B C) (CAR (CDR (CAR (CDR '(A (B C) D))))) => C
---Slide 11---
List Predicates

T = True, NIL = False
ATOM: True iff an atom
(ATOM 'NIL) => T (ATOM '(X Y)) => NIL
CONSP, LISTP,
NULL: True iff an empty list (e.g., NIL)
(NULL 'NIL) => T (NULL 'X) => NIL
ZEROP, NUMBERP, 
---Slide 12---
Conditional Form
IF & COND
(IF <test-predicate> <then-clause> <else-clause>)
(COND (<test-1> <result-1>)
(<test-2> <result-2>)
...
(<test-n> <result-n>)
)
IF form is equivalent to (COND (<test-predicate> <then-clause>)
(T <else-clause>))
COND, it
evaluates each of the test forms in order (i.e.,
<test-1>, <test-2>,
.
NIL,'' its
corresponding result form (i.e., <result-i>) is
evaluated and its value is returned as the result of the
COND.
---Slide 13---
Simple Examples
(IF (ATOM 'X) 'YES 'NO) => YES
(COND ((ATOM 'X) 'YES)
(T 'NO)) => YES
(DEFUN LENGTH (LIST)
(IF (NULL LIST)
0
(+ 1 (LENGTH (CDR LIST)))))
LENGTH Computation
(LENGTH '()) => 0 (LENGTH '(C)) = (+ 1 (LENGTH '())) => 1 (LENGTH '(B C)) = (+ 1 (LENGTH '(C))) => 2 (LENGTH '(A B C)) = (+ 1 (LENGTH '(B C))) => 3
---Slide 14---
LISP EVALUATOR: EVAL
S-expression (Symbolic Expn)
Values
Evaluation Order = ``Inside-Out''
NIL are constants and have predefined
values. Symbols can be SET to a value in the course of
execution.
(SET 'A '(X Y)) => A = (X Y)
CAR. The CDR of the list is treated as
the arguments.
Evaluation proceeds by applying the function (CAR) to
the argument (CDR)
(+ 2 4) => 6 (+ (CAR (CDR (LIST 2 3))) 4) => 7
---Slide 15---
QUOTE
If the symbol in the functional position is a special
form, then the usual evaluation rule is replaced by a
special one before the arguments are evaluated
QUOTE
Special Form
(QUOTE A) => 'A (QUOTE (A B (QUOTE C) D) => '(A B 'C D)
(SET 'PI 3.1415) => PI = 3.1415
=> 'PI = PI
(SETQ B 'X)
(SET B 'Y) => B = X
=> X = Y
(CONS 3 '(+ 5 6)) => (3 + 5 6)
(+ '3 '4) => 7
---Slide 16---
EVAL
(SETQ A (LIST '+ 5 6)) => A = (+ 5 6) (CONS A '(IS MY RESULT)) => ((+5 6) IS MY RESULT) (CONS (EVAL A) '(IS MY RESULT)) => (11 IS MY RESULT)
---Slide 17---
USER-DEFINED FUNCTIONS
(DEFUN <function-name> <argument-list> <body>)
DEFUN
<function-name>NIL
<argument-list>EVAL
procedure
<body>
---Slide 18---
Examples: APPEND & REVERSE
(DEFUN APPEND (X Y)
(IF (NULL X)
Y
(CONS (CAR X) (APPEND (CDR X) Y))))
(DEFUN REVERSE (LIST)
(IF (NULL LIST)
'NIL
(APPEND (REVERSE (CDR LIST))
(CONS (CAR LIST) 'NIL))))
[End of Lecture #11]