Anda di halaman 1dari 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 LISP interpreter LISP programs can also be compiled

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 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

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)

LISP

Arithmetic Functions
(+ numbers)
((* numbers) numbers)

(/

numbers)

(1+ number) (1- number) (abs number) (acos number)

LISP

(atan <number1> <number2>)


(cos .. (exp number (expt n1 n2) (gcd numbers) (log (max (min (random number) ---between 0 and number specified Etc.

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))

LISP

Exercises
Evaluate the following:
(+ 3 2) (+ 4 (/ 4 2 ) (* 2 4))

Create functions for: (3 + 4 + 5 + 6)(- 2 4)

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 lists together and take them apart.

LISP

Atoms and Lists


Atoms
Numbers, symbolic atom (anything not a number usually a list of characters

Lists
Anything with parentheses around it. () (a) (this is one too) (a list of (two) lists) (a (very (very (very (inscrutable) list)))

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 a value (e.g., a symbol, or a number). The CDR (also called rest) pointer usually points to the next CONS cell in a linked list.

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 Special Symbol: NIL


NIL represents an empty list. NIL is a terminator of a list.

A list is usually built by inserting its elements into NIL in the reverse order .
NIL can also represent false''.

The special symbol representing ``true'' is T.

LISP

Creating a CONS cell


The function CONS returns a newly created CONS 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

LISP

Using CONS to insert an item to the front of a list


(CONS <item to be inserted> <a list>)
Example: (CONS 'John (CONS 'loves (CONS 'Mary NIL))) returns

* First CONS call returns (MARY )


* Second CONS call returns ( LOVES MARY ) * Third CONS call returns ( JOHN LOVES MARY )

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

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) books on Internet)) returns '(sells

-> ( ( AMAZON BN ) SELLS BOOKS ON INTERNET )

LISP

Taking lists apart


(CAR <a list>) returns the first element of the list.
(CDR <a list>) 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)

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.

Example: (quote (dummy-fn 2))


==> (DUMMY-FN 2)

LISP

Other List Creating Functions


(LIST <item1> <item2> ... <itemN>) returns a list whose first element is <item 1>, second element is <item 2>, ...., Nth element is <itemN>. Example: (LIST 'John 'loves Michelle) returns (JOHN LOVES MICHELLE) Example: (LIST 'John 'loves (LIST AI research)) returns (JOHN LOVES (AI RESEARCH))

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

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.

LISP

Assignment and Binding


A symbol (or variable) can be assigned a value (called its binding) using SETQ.
(SETQ <symbol-name> <anything>) 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 error Ex: B evaluates to ==> Error: Unbound Variable

LISP

All other functions do NOT change the bindings


In particular, CAR and CDR are nondestructive.
> (setq my-friends (Superman Batman Robin) )

(Superman Batman Robin)


> (car (cdr my-friends)) Batman > my-friends (Superman Batman Robin)

LISP

All other functions do NOT change 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)

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

7. (tom jerry donald micky) 8. (+ 3 5 6)

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

Defining My-Own Functions


A function is defined using DEFUN
(DEFUN <fn-name> (<arg1> ...<argK>) <exp1> ... <expN> )

All arguments are passed by value.


The body of the function may contain any number of expressions (i.e., function calls).

The function returns the value returned by the last expression.

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)

LISP

Changing a symbols binding does not change its function def.


>(setq cons list)
LIST >(setq b (cons cons nil)) (LIST) > cons LIST > (cons cons b) (CONS LIST)

LISP

Predicates Checking to see if something is true.


Functions that return ``true (i.e., T) or ``false (i.e., NIL). type-testing predicates (NULL <item>) returns T if <item> is NIL (empty list), otherwise NIL. (LISTP <item>) returns T if <item> is a list, otherwise NIL.

(ATOM <item>) returns T if <item> is an atom (i.e., a symbol, a number, or NIL).


(NUMBERP <item>) returns T if <item> is a number

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

> (member (Batman Robin) 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

Terminates a COND with a T condition


(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

Building More Complex Conditions


Simple tests can be combined into a complex one using AND, OR, NOT. (AND <test1> <test2> ... <testn>) evaluates tests in a leftto-right order. It returns NIL when it encounters the first test that evaluates to NIL (remaining tests are not evaluated). If all the tests return non-NIL, AND returns the value of the last test (i.e., testn).
(defun safe-caar ( L ) (and (listp L) (listp (car L)) (car (car L) ) ))

LISP

More Examples of AND


> (safe-caar A)
NIL > (safe-caar ( ( A ) ) ) A > (caar A) Error!

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

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

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

Write a function that is invoked as:


(asc-sqr <a> <b> <c>) returns t if a square = b and b square = c Write a function to determine a students letter grade given a number (grade <b>) returns A= 100-90; B= 89-80;etc.

Anda mungkin juga menyukai