Advance LISP (Artificial Intelligence)

26
Artificial Intelligence [LISP] Chpter # 9-B Wahab Khan

Transcript of Advance LISP (Artificial Intelligence)

Page 1: Advance LISP (Artificial Intelligence)

Artificial Intelligence [LISP]

Chpter # 9-B

Wahab Khan

Page 2: Advance LISP (Artificial Intelligence)

LISP• Invented by John McCarthy (1958)• Two simple data structure (atoms and lists)• Heavy use of recursion• Interpretive language• Variations

– Scheme– Common Lisp (de facto industrial standard)

• Most widely used AI programming language• Functional Programming Paradigm• Low maintenance overhead

Page 3: Advance LISP (Artificial Intelligence)

• Atoms (Can’t be sub-divided) may be; • Numbers: (real 1.0, integer 1)• Symbols: a consecutive sequence of characters (no

space)e.g. a, x, price-of-beef

• Two special symbols: T and NIL for logical true and false

• Strings: a sequence of characters bounded by double quotes e.g. "this is red"

Valid Objects

Page 4: Advance LISP (Artificial Intelligence)

• Lists: A list of atoms and/or lists, bounded by ( and ). For example (a b c) (a (b c))

• Top elements of a list – first level Example:

Top elements of list (a b c) are a, b, and c Top elements of list (a (b c)) are a and (b c) ‘nil’ stands for empty list, same as ()

Valid Objects

Page 5: Advance LISP (Artificial Intelligence)

• Its also a list• Uses prefix notation:

(function-name arg1,arg2 ... argN)

• It returns function value for the given list of arguments

• Functions are either provided by LISP function library or defined by the user

Function Calls

Page 6: Advance LISP (Artificial Intelligence)

• Examples:

> (+ 1 3 5) 9

> (/ 3 5) 3/5

> (/ 3.0 5) 0.59999999999999998

> (sqrt 4) 2

Function Calls

Command Line Prompt

Page 7: Advance LISP (Artificial Intelligence)

1) Evaluating an atom

• Numerical and string atoms evaluate to themselves

• Symbols evaluate to their values if they are assigned values, otherwise return Error

• The values of T and NIL are themselves

Evaluation of S-expression

Page 8: Advance LISP (Artificial Intelligence)

• Examples :

> (sqrt x)Error: The variable X is unbound

> (+ (sqrt 4) 4.0)6.0

> (+ (/ 3 5) 4)23/5

Evaluation of S-expression3/5+4

Page 9: Advance LISP (Artificial Intelligence)

3) To assign a value to a symbol use: setq, set, setf

•‘setq’ is a special form of function (with two arguments)

The first argument is a symbol which will not be evaluated

The second argument is a S-expression, which will be evaluated

The value of the second argument is assigned to be the value of the first argument

> (setq x 3.0) 3.0> x3.0

Evaluation of S-expression

Page 10: Advance LISP (Artificial Intelligence)

> (setq y x) 3.0

> y3.0

> (+ x y) 6.0

Page 11: Advance LISP (Artificial Intelligence)

• To forbid evaluation of a symbol use: quote or ’

> (quote x) x> 'xx> (setq z 'x) x

Page 12: Advance LISP (Artificial Intelligence)

Functions• Math functions

– +, -, *, /, exp, expt, log, sqrt, sin, cos, tan, max, min

• Functions for creating lists or ‘List Constructors’ – Cons, list and append

> (cons 'x L) ; insert symbol x at the front of list L(X A B C) > (setq L '(A B C))

> (list 'a 'b 'c) ; making a list with the arguments as its elements

(A B C) ; if a, b, c have values A, B, C, then (list a b c) ; returns list (A B C)

> (append '(a b) '(c d)) ; appends one list in front of another (A B C D)

L is a list containing elements (A,B,C)

X is inserted at the front/top of the list

Page 13: Advance LISP (Artificial Intelligence)

Functions (cont’)• Selectors

– first, rest, nth, car, cdr…> (first '(a s d f)) ;returns the first element of a list a

> (rest '((a s) d f)) ;deletes the first element from the

;list and returns the rest as a list(d f)

> (first '((a s) d f)) (a s)

> (rest '((a s) (d f)) ((d f))

Page 14: Advance LISP (Artificial Intelligence)

> (setq L '(A B C)) (A B C)

> (car L) ; returns the first top level element of list LA

> (cdr L) ; returns the rest of list L(B C)

Page 15: Advance LISP (Artificial Intelligence)

Functions> (reverse L) ; reverses a list (C B A)

> (length L) ; returns the length of list L 3

List has elements (A B C)

Page 16: Advance LISP (Artificial Intelligence)

• Conditional control: – if, when and cond

(if <test> <then> <else>)

> (setq SCORE 78)> 78> (if (> score 85) ‘HIGH

(if (and (< score 84) (> score 65)) ‘MEDIUM ‘LOW))

> MEDIUM

Control Flow

score<84 and score>65

Page 17: Advance LISP (Artificial Intelligence)

Predicates• A special function which returns

NIL if the predicate is false, T or anything other than NIL, otherwise

• We can use the following operators to build the predicates– =, >, <, >=, <= for numeric values – eq or equal for others– atom: test if x is an atom– listp: test if x is a list

Page 18: Advance LISP (Artificial Intelligence)

• A list can be viewed as a set whose members are the top elements of the list

> (member 'b L) (B C)

> (member ‘b (cons 'b L)) (B A B C)

> (member x L) NIL ; if no, returns NIL

> (union L1 L2) ; returns the union of the two lists > (intersection L1 L2) ; returns the intersection of the two lists > (set-difference L1 L2) ; returns the difference of the two lists

Set Operations

Test if symbol b is a member (a top element) of L; if yes, returns the sublist of L starting at the first occurrence of symbol b

List L has elements (A B C)

Page 19: Advance LISP (Artificial Intelligence)

Defining LISP functions(defun func-name (arg-1 ... Arg-n) func-

body)Example:

> (defun y-plus (x) (+ x y)) ;definition of y-plus

> (setq y 2)> (y-plus 23)25

Page 20: Advance LISP (Artificial Intelligence)

• Recursion– Main tool used for iteration– There is a limit to the depth of the

recursion, and it depends on the version of LISP

– Use trace to watch recursion

– Example:(defun power (x y)

(if (= y 0) 1 (* x (power x (1- y)))))

Page 21: Advance LISP (Artificial Intelligence)

examples: (defun member (x L) (cond ((null L) nil) ; base case 1: L is empty ((equal x (car L)) L) ; base case 2: x=first(L) (t (member x (cdr L))) ; recursion: test if x is in rest(L) ))

(defun intersection (L1 L2) (cond ((null L1) nil) ((null L2) nil) ((member (car L1) L2) (cons (car L1) (intersection (cdr L1) L2)))

(t (intersection (cdr L1) L2)) ))

> (intersection '(a b c) '(b a b c)) > (a b c)

> (intersection '(b a b c) '(a b c)) > (b a b c)

Page 22: Advance LISP (Artificial Intelligence)

• Iteration: dotimes and dolist– (dolist (x L result) body)

• for each top level element x in L, do body(x);• x is not equal to an element of L in each iteration, but

rather x takes an element of L as its value;

– (dotimes (count n result) body) • do body n times. count starts with 0, ends with n-1

– Note: result is optional, to be used to hold the computing result. If result is given, the function will return the value of result, returns NIL, otherwise. (may change global variables as side effects.)

Page 23: Advance LISP (Artificial Intelligence)

Arrays• Data type to store expressions in places

identified by integer indexes.

• (setf linear-array (make-array ‘(4)))• A single dimension array by the name of

linear-array with indices from 0 to 3.

• (setf linear-array (make-array 4))• (setf chess-board (make-array ‘(8 8)))

Page 24: Advance LISP (Artificial Intelligence)

• Other functions in LISP library 1) Predicates:zerop, plusp, evenp, oddp, integerp, floatp 2) Logical connector: and, or, not 3) Rounding: floor, ceiling, truncate, round 4) Others: max, min, abs, sqrt, 1+ (add 1), 1- (minus 1)

(exp number) (base-e exponential) (expt Base-number Power-Number) (log number & Optional base-number)

(isqrt number) Returns the greater integer less than or equal to the exact positive square-root of the number.

(signum number) Returns -1, zero, or 1 according if the number is negative, zero, or positive.

Page 25: Advance LISP (Artificial Intelligence)

Summary• Atoms and lists• Functions and function calls setq, setf, set, quote, eval,

math functions (+, -, *, /, max, min, exp, sqrt, …)list operations: list, cons, car, cdr, length, nth, append, reversepredicates (=, >, equal, eq, numberp, symbolp, …)

• Defining functions(defun func_name (arg_list) func_body)dolist, dotimes, cond, if, when, unless, mapcar

• Input/output: print, read, with-open-file, load

Page 26: Advance LISP (Artificial Intelligence)

What made Lisp Different• Conditionals: such as if-then-else construct.• Function types: where functions are just like

integers and strings• Recursion: first language to support it.• Dynamic typing: all variable are pointers. • Garbage-Collection.• Programs composed of expressions.• A symbol type.• The whole program is a mathematical function