lisp-i
TRANSCRIPT
-
7/28/2019 lisp-i
1/45
LISP
What is LISP?
A LISt Processing language
The basic data structure is linked list
A functional programming language Each expression in LISP is a function that returns a value
A good LISP program should avoid too many side effects
An interpretive language
Running LISP programs involves interacting with the LISPinterpreter
LISP programs can also be compiled
-
7/28/2019 lisp-i
2/45
LISP
Why LISP for AI Programming?
LISP supports symbol manipulation better
Symbols are the basic entity of the language
The interpretive nature makes it easy to try anew 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 developedin PROLOG
-
7/28/2019 lisp-i
3/45
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: (print 'hello)
returns
Hello
Hello
-
7/28/2019 lisp-i
4/45
LISP
Read-Eval-Loop
Evaluation process
(+ 3 4)
Parentheses Notification to evaluate
Function name Go get function (in thiscase + is the add function)
space - separatoroperands -- value for function
Parentheses stop evaluation
-
7/28/2019 lisp-i
5/45
LISP
How do I do math in lisp?
Use built in arithmetic functions
Put the arithmetic functions together
Examples:
4 + 5
4 + 5 + 6
(4 + 5 + 5) (2 + 2)
-
7/28/2019 lisp-i
6/45
LISP
Arithmetic Functions
(+ numbers)
(- numbers)
(* numbers)(/ numbers)
(1+ number)
(1- number)(abs number)
(acos number)
-
7/28/2019 lisp-i
7/45
LISP
(atan )
(cos ..
(exp number
(expt n1 n2)
(gcd numbers)
(log
(max(min
(random number) ---between 0 and number specified
Etc.
-
7/28/2019 lisp-i
8/45
LISP
Back to the Examples:
Examples:
4 + 5 -> (+ 4 5)
4 + 5 + 6 -> (+ 4 5 6)
(4 + 5 + 5) (2 + 2) (* (+ 4 5 5) (+ 2 2))
-
7/28/2019 lisp-i
9/45
LISP
Exercises
Evaluate the following:
(+ 3 2)
(+ 4 (/ 4 2 ) (* 2 4))
Create functions for:
(3 + 4 + 5 + 6)(- 2 4)
-
7/28/2019 lisp-i
10/45
LISP
How do I process words,characters, symbols?
Examples of problems:
Is the word kathy in the list that the user
entered?How many words are there in the sentence?
Answer: I use built-in lisp functions to put liststogether and take them apart.
-
7/28/2019 lisp-i
11/45
LISP
Atoms and Lists
Atoms
Numbers, symbolic atom (anything not a number usually a listof characters
ListsAnything with parentheses around it.
()
(a)
(this is one too)
(a list of (two) lists)
(a (very (very (very (inscrutable) list)))
-
7/28/2019 lisp-i
12/45
LISP
The Building Block of Lists:
The CONS Cell A CONS cell contains two pointer: CAR and CDR.
The CAR (also called first) pointer usually points to avalue (e.g., a symbol, or a number).
The CDR (also called rest) pointer usually points to thenext CONS cell in a linked list.
CAR CAR
CDRCDR
-
7/28/2019 lisp-i
13/45
LISP
Basics of Lists
A linked list always terminates by having theCDR of the last CONS cell point to a specialsymbol: NIL.
Example: ( John loves Mary )
John loves Mary
NIL
-
7/28/2019 lisp-i
14/45
LISP
A Special Symbol: NIL
NIL represents an empty list.
NIL is a terminator of a list.
A list is usually built by inserting its elementsinto NIL in the reverse order .
NIL can also represent false''.
The special symbol representing ``true'' is T.
-
7/28/2019 lisp-i
15/45
LISP
Creating a CONS cell
The function CONS returns a newly createdCONS cell.
The function takes two arguments.
The CAR pt of the new cell points to the first argument.
The CDR pt of the new cell points to the second argument.
Example: (CONS Mary NIL) returns
Mary
NIL> ( MARY )
-
7/28/2019 lisp-i
16/45
LISP
Using CONS to insert an itemto the front of a list
(CONS )
Example: (CONS 'John (CONS 'loves (CONS'Mary NIL))) returns
* Third CONS call returns ( JOHN LOVES MARY )
* Second CONS call
returns ( LOVES MARY )
* First CONS call
returns (MARY )
-
7/28/2019 lisp-i
17/45
LISP
Nested Sublists
A sublist is a list pointed by a CAR pointer ofa CONS cell.
Example: ( John loves ( play tennis ) )
John loves
NIL
NIL
play tennis
-
7/28/2019 lisp-i
18/45
LISP
Using CONS to Insert Sublists
When CONS inserts a list into another list.The former becomes a sublist of the latter.
Example: (CONS '(Amazon BN) '(sellsbooks on Internet)) returns
->( ( AMAZON BN ) SELLS BOOKS ONINTERNET )
-
7/28/2019 lisp-i
19/45
LISP
Taking lists apart
(CAR ) returns the first element of thelist.
(CDR ) returns the remaining list (i.e.,
everything except the first element).
Example: (CAR '(John loves Michelle) ) returns
> JOHN
Example: (CDR '(John loves Michelle) )
returns> (LOVES MICHELLE)
-
7/28/2019 lisp-i
20/45
LISP
Quote
Quote symbol is a short hand for a functionQUOTE.
(QUOTE ) QUOTE is a special function that prevents
LISP from evaluating its argument.
QUOTE returns the argument literately.
Example: (quote (dummy-fn 2))
==> (DUMMY-FN 2)
-
7/28/2019 lisp-i
21/45
LISP
Other List Creating Functions
(LIST ... ) returns alist whose first element is , secondelement is , ...., Nth element is.
Example: (LIST 'John 'loves Michelle) returns(JOHN LOVES MICHELLE)
Example:(LIST 'John 'loves (LIST AI research)) returns(JOHN LOVES (AI RESEARCH))
-
7/28/2019 lisp-i
22/45
LISP
Concatenating Lists
(APPEND ) returns a list that isthe result of concatenating and.
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)
-
7/28/2019 lisp-i
23/45
LISP
Evaluation
LISP executes/interprets an expressionthrough an evaluation procedure.
Example:
(+ 3 5) evaluates to ==> 8
(CONS A NIL) evaluates to ==> (A)
3 evaluates to ==> 3A evaluates to ==> A
-
7/28/2019 lisp-i
24/45
LISP
Basic Evaluation Rules
A number evaluates to itself
A symbol evaluates to its value.
SETQ assigns a value to a symbol
A list is evaluated by
treating the first element as a function
evaluating each arguments of the function in a left-to-right
order
An expression preceded by a quote symbol evaluates to the expression itself.
-
7/28/2019 lisp-i
25/45
LISP
Assignment and Binding
A symbol (or variable) can be assigned avalue (called its binding) using SETQ.
(SETQ )
Example: (SETQ A (A B C)) ==> (A B C)
A evaluates to ==> (A B C)
Evaluating a symbol that does not have a
value assigned (i.e., no binding) causes errorEx: B evaluates to ==> Error: Unbound
Variable
-
7/28/2019 lisp-i
26/45
LISP
All other functions do NOTchange the bindings
In particular, CAR and CDR are non-destructive.
> (setq my-friends (Superman Batman Robin) )
(Superman Batman Robin)
> (car (cdr my-friends))
Batman> my-friends
(Superman Batman Robin)
-
7/28/2019 lisp-i
27/45
LISP
All other functions do NOTchange the bindings (cont.)
CONS does not change bindings either.
> (CONS J-Bond my-friends)
(J-Bond Superman Batman Robin)
> my-friends
(Superman Batman Robin)
> (setq my-friends (CONS J-Bond friends) )
(J-Bond Superman Batman Robin)
> my-friends
(J-Bond Superman Batman Robin)
-
7/28/2019 lisp-i
28/45
LISP
Exercises
Which of the following are atoms, which lists, which bothand which neither?
1. nil3. (expt 10 3)4. (a b)5. 646. t7. (tom jerry donald micky)
8. (+ 3 5 6)
-
7/28/2019 lisp-i
29/45
LISP
What is returned by each of the followingexpressions (assume they are evaluated inthe 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))
-
7/28/2019 lisp-i
30/45
LISP
Defining My-Own Functions
A function is defined using DEFUN
(DEFUN ( ...) ... )
All arguments are passed by value.
The body of the function may contain anynumber of expressions (i.e., function calls).
The function returns the value returned by thelast expression.
-
7/28/2019 lisp-i
31/45
LISP
Defining A Function
(defun square (x)
(times x x) )
(defun add-friend (new-friend friends)
(cons new-friend friends) )
After defining it, I call the function!!!!
-> (square 5)
25
-> (add-friend B-Gates my-friends)
(B-Gates J-Bond Superman Batman Robin)
-
7/28/2019 lisp-i
32/45
LISP
Changing a symbols binding does notchange its function def.
>(setq cons list)
LIST
>(setq b (cons cons nil))
(LIST)
> cons
LIST
> (cons cons b)
(CONS LIST)
-
7/28/2019 lisp-i
33/45
LISP
Predicates Checking to see ifsomething is true.
Functions that return ``true (i.e., T) or``false (i.e., NIL).
type-testing predicates
(NULL ) returns T if is NIL(empty list), otherwise NIL.
(LISTP ) returns T if is a list,otherwise NIL.
(ATOM ) returns T if is an atom(i.e., a symbol, a number, or NIL).
(NUMBERP ) returns T if is anumber
-
7/28/2019 lisp-i
34/45
LISP
Equality-testing Predicates
(EQ ) returns T if twoarguments are two identical atoms.
(EQUAL ) returns T if two
arguments are identical atoms or identicallists.
> (EQ (Banana Apple) (Banana Apple))
NIL> (EQUAL (Banana Apple) (Banana Apple))
T
-
7/28/2019 lisp-i
35/45
LISP
Other Predicates
(MEMBER ) returns the elements if isan atom that is a top-level element of.
> (setq my-friends (MacGyver (Batman Robin)) )
> (member MacGyver my-friends)
macgyver
> (member Batman my-friends)
NIL
> (member (Batman Robin) my-friends)
NIL
-
7/28/2019 lisp-i
36/45
LISP
Conditional Expression
COND is an N-branch conditional expression
(COND ( ... )
( ... )
...
( ... ) )
Each test is evaluated sequentially until a testreturns true.
Expressions following that test will beexecuted.
COND returns the value returned by the lastexpression associated with the test.
-
7/28/2019 lisp-i
37/45
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) ))
)
-
7/28/2019 lisp-i
38/45
LISP
Terminates a COND with a Tcondition
(defun select-character (enemy)(cond ( (eq enemy Penguin)
Batman)( (eq enemy Catwoman)
J-Bond )( (eq enemy Black-Knight)
(White-Knight King-Arthur ) )( T ; for all other enemies
SuperMan) ; ask Superman for help)
)
LISP
-
7/28/2019 lisp-i
39/45
Building More ComplexConditions
Simple tests can be combined into a complex oneusing AND, OR, NOT.
(AND ... ) evaluates tests in a left-to-right order. It returns NIL when it encounters the
first test that evaluates to NIL (remaining tests are notevaluated). If all the tests return non-NIL, AND returnsthe value of the last test (i.e., testn).
(defun safe-caar ( L )
(and (listp L)
(listp (car L))(car (car L) )
) )
LISP
-
7/28/2019 lisp-i
40/45
More Examples of AND
> (safe-caar A)
NIL
> (safe-caar ( ( A ) ) )
A
> (caar A)
Error!
LISP
-
7/28/2019 lisp-i
41/45
OR and NOT
(OR ... ) evaluates testsin a left-to-right order. It returns T when itencounters the first test that evaluates to T
(remaining tests are not evaluated). If all thetests return NIL, OR returns NIL.
(NOT ) returns T if is NIL, returnsNIL if is non-NIL.
> (NOT (NULL (A B C) ) )
T
LISP
-
7/28/2019 lisp-i
42/45
Using AND, OR in COND
(defun Evaluate-feeling ( sentence )
(cond ( (OR (member hate sentence)
(member dislike sentence))
hatred)( (AND (member I sentence)
(member feel sentence) )
self-centered )
( T
happy)
) ; end of cond
) ; end of defun
LISP
-
7/28/2019 lisp-i
43/45
problems
Rewrite the following in lisp
23 * 2
45/9 3
50 1020
1.0/2.0
5 * [20-12/2]
LISP
-
7/28/2019 lisp-i
44/45
(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) may
Not (injure a (human) being)
(human) (((or)))Or allow
Harm nil
LISP
-
7/28/2019 lisp-i
45/45
Write a function that is invoked as:
(asc-sqr )
returns t if a square = b and b square = c
Write a function to determine a students lettergrade given a number
(grade )
returns A= 100-90; B= 89-80;etc.