Академический Документы
Профессиональный Документы
Культура Документы
What is LISP?
A LISt Processing language
The basic data structure is linked list
An interpretive language
Running LISP programs involves interacting with the LISP interpreter LISP programs can also be compiled
LISP
The interpretive nature makes it easy to try a new idea and prototype Functions are in the form of linked lists
Easier to write learning programs
Historical Reason
Most AI programs in the U.S. have been developed in LISP However, most AI programs in Europe have been developed in PROLOG
LISP
Functions
Lisp is a functional language
-> sqrt(x) In lisp this function is: -> (sqrt x)
Prefix Notation
Interpretive Primary effect vs. side effects
Example: returns Hello Hello (print 'hello)
LISP
Read-Eval-Loop
Evaluation process
(+ 3 4) Parentheses Notification to evaluate Function name Go get function (in this case + is the add function) space - separator operands -- value for function Parentheses stop evaluation
LISP
LISP
Arithmetic Functions
(+ numbers)
((* numbers) numbers)
(/
numbers)
LISP
LISP
LISP
Exercises
Evaluate the following:
(+ 3 2) (+ 4 (/ 4 2 ) (* 2 4))
LISP
LISP
Lists
Anything with parentheses around it. () (a) (this is one too) (a list of (two) lists) (a (very (very (very (inscrutable) list)))
LISP
CDR
CAR CAR
CDR
LISP
Basics of Lists
A linked list always terminates by having the CDR of the last CONS cell point to a special symbol: NIL.
Example: ( John loves Mary )
NIL
John
loves
Mary
LISP
A list is usually built by inserting its elements into NIL in the reverse order .
NIL can also represent false''.
LISP
> ( MARY )
NIL
Mary
LISP
LISP
Nested Sublists
A sublist is a list pointed by a CAR pointer of a CONS cell.
Example: ( John loves ( play tennis ) )
NIL
John
loves
NIL
play
tennis
LISP
LISP
LISP
Quote
Quote symbol is a short hand for a function QUOTE. (QUOTE <arg>)
QUOTE is a special function that prevents LISP from evaluating its argument.
QUOTE returns the argument literately.
LISP
LISP
Concatenating Lists
(APPEND <list 1> <list 2>) returns a list that is the result of concatenating <list 1> and <list 2>.
Example: (APPEND '(Orange Apple) '(Grape)) returns (ORANGE APPLE GRAPE) APPEND can also be used to concatenate more than two lists. Example: (APPEND '(play) '(tennis) '(football baseball) ) ) ) returns (PLAY TENNIS FOOTBALL BASEBALL)
LISP
Evaluation
LISP executes/interprets an expression through an evaluation procedure.
Example:
(+ 3 5) evaluates to
(CONS A NIL) evaluates to 3 evaluates to A evaluates to
==> 8
==> (A) ==> 3 ==> A
LISP
A list is evaluated by
treating the first element as a function evaluating each arguments of the function in a left-to-right order
LISP
LISP
LISP
> my-friends
(Superman Batman Robin) > (setq my-friends (CONS J-Bond friends) )
LISP
Exercises
Which of the following are atoms, which lists, which both and which neither? 1. 3. 4. 5. 6. nil (expt 10 3) (a b) 64 t
LISP
What is returned by each of the following expressions (assume they are evaluated in the given order)?
a. (setq trek '(picard riker laforge worf)) b. (cons 'data trek) c. (append data trek)
d. trek
d. (setq trek (cons 'data trek))
LISP
LISP
Defining A Function
(defun square (x) (times x x) ) (defun add-friend (new-friend friends)
LISP
LISP
LISP
Equality-testing Predicates
(EQ <item1> <item2>) returns T if two arguments are two identical atoms.
(EQUAL <item1> <item2>) returns T if two arguments are identical atoms or identical lists. > (EQ (Banana Apple) (Banana Apple)) NIL > (EQUAL (Banana Apple) (Banana Apple)) T
LISP
Other Predicates
(MEMBER <item> <list>) returns the elements if <item> is an atom that is a top-level element of <list>.
> (setq my-friends (MacGyver (Batman Robin)) ) > (member MacGyver my-friends) macgyver > (member Batman my-friends) NIL
LISP
Conditional Expression
COND is an N-branch conditional expression (COND ( <test1> <exp11> ... <exp1L> ) ( <test2> <exp21> ... <exp2M> ) ... ( <testK> <expK1> ... <expKN> ) ) Each test is evaluated sequentially until a test returns true. Expressions following that test will be executed. COND returns the value returned by the last expression associated with the test.
LISP
Example of COND
(defun select-character (enemy) (cond ( (eq enemy Penguin) Batman) ( (eq enemy Catwoman) J-Bond ) ( (eq enemy Black-Knight) (White-Knight King-Arthur) ) ) )
LISP
LISP
LISP
LISP
OR and NOT
(OR <test1> <test2> ... <testn>) evaluates tests in a left-to-right order. It returns T when it encounters the first test that evaluates to T (remaining tests are not evaluated). If all the tests return NIL, OR returns NIL.
(NOT <exp> ) returns T if <exp> is NIL, returns NIL if <exp> is non-NIL. > (NOT (NULL (A B C) ) ) T
LISP
LISP
problems
Rewrite the following in lisp
23 * 2 45/9 3 50 10 20 1.0/2.0 5 * [20-12/2]
LISP
(setq law1 ((a robot) may not (injure a (human) being) (((or))) (through inaction) allow a (human being) (((to) come) to) harm))
Give me the series of cars and cdrs to retrieve the following
(a robot)
Not (human) Or Harm
may
(injure a (human) being) (((or))) allow nil
LISP