16
BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE PILANI-333031 (RAJASTHAN) INDIA Second Semester 2007-2008 Course: EA C461 (Artificial Intelligence) Lab 6: Lisp Programming and Working with Lisp Studio Prepared By: Dr. Mukesh Kumar Rohil [email protected] An Introduction to LISP LISP is otherwise known as List Processing. The main features of LISP are: Flexibility : the program structure can be changed. Symbolic Computations : frames and semantic networks can be implemented. Components of a LISP program : Atom : number or a sequence of symbols(characters). Ex. a, b, 1, 2. NOTE : An atom cannot start with a number. List : a set of objects containing numbers or sublists Ex. (a b), (a (1 b)), ( ). String : characters enclosed with double quotes. Ex. “a”, “ab” .A LISP program is executed by the Read-Evaluate-Print cycle. Ex.(1) > (+ 2 3) 5 (2)> (> (* 5 6) (+ 4 5))

Lab 6 Lisp Programming and Working With Lisp Studio

Embed Size (px)

Citation preview

Page 1: Lab 6 Lisp Programming and Working With Lisp Studio

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCEPILANI-333031 (RAJASTHAN) INDIA

Second Semester 2007-2008

Course: EA C461 (Artificial Intelligence)

Lab 6: Lisp Programming and Working with Lisp Studio

Prepared By: Dr. Mukesh Kumar Rohil [email protected]

An Introduction to LISP

LISP is otherwise known as List Processing. The main features of LISP are:

Flexibility : the program structure can be changed.

Symbolic Computations : frames and semantic networks can be implemented.

Components of a LISP program :

Atom : number or a sequence of symbols(characters).

Ex. a, b, 1, 2.

NOTE : An atom cannot start with a number.

List : a set of objects containing numbers or sublists

Ex. (a b), (a (1 b)), ( ).

String : characters enclosed with double quotes.

Ex. “a”, “ab”

.A LISP program is executed by the Read-Evaluate-Print cycle.

Ex.(1) > (+ 2 3)

5

(2) > (> (* 5 6) (+ 4 5))

T (This means that the symbolic expression is true)

NOTE : The “>” symbol denotes the LISP prompt like a $ for the UNIX prompt.

LISP operators

Numeric Operators

+ : addition, * : multiplication -: subtraction, / :division.

Q: Try out the following at the LISP prompt :

(i) >(+ )

(ii) >(* )

Quote and Eval

Page 2: Lab 6 Lisp Programming and Working With Lisp Studio

The Quote operator takes one argument and returns it without evaluating it.

>(quote (* 2 3)

(* 2 3)

The Eval operator undoes the effect of Quote and evaluates the expression.

>(eval (quote (+ 2 3))

5

List : creating a list

> (list 2 5)

(2 5)

Cons : adding an atom or a list to another list

> (cons ‘(a) ‘(b c))

((a) b c)

Car : returns the first element of a list

> (car ‘(a b c))

a

Cdr : retrieves the list without the first element

> (cdr ‘( (a) b c (d) ))

(b c (d) )

Last : returns the last element of a list

>(last ‘(a b (c)))

(c)

Member : returns TRUE if the first argument is a part of the second argument and

returns the second list starting from the first argument

> (member ‘b ‘(a b (c) d))

(b (c) d)

Reverse : reverses a list

> (reverse ‘(a b (c d) e))

(e (c d) b a)

Setq : assigns an atom or a list to a variable

> (setq a ‘(1 2 3))

(1 2 3)

> (setq b (cons 0 (cdr a)))

(0 2 3)

Page 3: Lab 6 Lisp Programming and Working With Lisp Studio

Logic Functions

And : returns TRUE only if all the conditions given as arguments are valid

> (and (> 2 3) ( < 1 2))

NIL

> (and (> 3 2) (< 1 2) (listp ‘(1 2 3)))

T

Or : returns NIL only if all the conditions are false.

> (or (< 2 3) (> 3 4 ))

T

> (or (listp ‘a) (> 3 4) (< 2 1))

NIL

Not : negates the argument

> (not nil)

T

Predicate Functions

Equal : returns TRUE if the two arguments have the same value otherwise NIL

> (setq x ‘(1 2 3))

(1 2 3)

> (setq y ‘(1 2 3))

(1 2 3)

> (equal x y )

T

Eq : returns TRUE if the two arguments point to the same memory location.

If Eq command is used in place of Equal in the previous program it returns NIL.

Q. Try to think in which cases Eq command can return a TRUE value.

Atom : returns TRUE if the argument is an atom

> (atom ‘(1 2 ))

NIL

Null : returns TRUE if the list is null

> (null nil)

T

Length : returns the length of the list

Page 4: Lab 6 Lisp Programming and Working With Lisp Studio

>(length ‘(1 2 (3 4)))

3

User-Defined Functions

Defun : one can define one’s own function by this command.

The syntax for this command is

> (defun function-name (p1 p2 … )

symbolic expressions

body

)

FUNCTION-NAME

This returns the function name if it is successful otherwise generates an error.

Ex. Function to calculate the average of three numbers

> (defun AVG ( a b c)

(/ (+ a b c) 3)

)

AVG

> (AVG 1 3 5)

3

Conditional Statements

Cond : it is used in conditional statements where there are several possibilities.

The syntax is as follows :

> (cond ( <test1> <act1>)

( <test2> <act2>)

……………….

( <testn> <actn>)

)

Ex. Maximum of three numbers

> (defun MAX (a b c)

(cond ( (> b a) (cond (> c a) a)

(t c))

( (> c b) b)

Page 5: Lab 6 Lisp Programming and Working With Lisp Studio

( t c)

)

)

MAX

Iteration

Do : The syntax is as follows

> (do (<var1> <val1> <var_up1>)

………………………..

(<varn> <valn> <var_upn>)

(<test> <return_value>)

(<symbolic expression>)

)

Ex. Factorial of a number n

> (defun fact (n)

(do (count n (- count 1)

(product 1 (* product (- count 1)))

(equal count 1) product)

)

)

FACT

Recursion

Lisp Functions can also be used in a recursive manner.

Ex. Counting the number of atoms in a list

> (defun countatom(lst)

(cond ( (null lst) 0)

( (atom lst) 1)

( t (+ (countatom(car lst)) (countatom(cdr lst)) ))

)

)

COUNTATOM

Q. Try to implement the Factorial Problem using Recursion.

Page 6: Lab 6 Lisp Programming and Working With Lisp Studio

Property Lists

Putprop : used to set values to a certain attribute of an object

The syntax is as follows

> ( putprop object value attribute)

Ex. > (putprop ‘Harry ‘3R ‘class)

3R

> (putprop ‘Harry ‘(10 20 30) ‘marks)

(10 20 30)

> (get ‘Harry ‘class)

3R

Remprop : used to remove the value from an attribute

Ex. > (remprop ‘Harry ‘marks)

NIL

>(get ‘Harry ‘marks)

NIL

Mapping Functions

Mapcar : used to map a function over a given list

> (mapcar ‘1+ ‘(2 4 5))

(3 5 6)

> (mapcar ‘+ ‘(2 3 5) ‘(4 5))

(6 8)

Lambda : it is an unnamed or anonymous function which allows one to separate a

function definition from the function name.

> (mapcar #’(lambda (x) (* x x) ) ‘(2 3 4))

(4 9 16)

> (mapcar #’(lambda (x) (and (> x 0) (< x 10))) ‘( 1 24 5 -9 8 50))

(T NIL T NIL T NIL)

Page 7: Lab 6 Lisp Programming and Working With Lisp Studio

Working in LISP Studio

The most widely used environment used for LISP programming is the LISP Studio

because of its user-friendly interface and ability to compile complex LISP codes.

This is the screen that one gets as one double-clicks on the LISP Studio icon.

We can write any LISP command on the bottom line of the terminal and it shall be

executed at the LISP prompt “>” shown above.

Page 8: Lab 6 Lisp Programming and Working With Lisp Studio

For writing long programs or defining any functions just click on File and then New,

which shall open a new module as follows:

One can write the LISP program in the module and then save it by the Save command

in the File drop down menu.

Page 9: Lab 6 Lisp Programming and Working With Lisp Studio

After saving the LISP code we can execute by the Compile command in the Project

drop down menu and then the Run command also in the Project drop down menu.

If the code is written properly without any errors then the name of the user-defined

function will be shown in the terminal.

Page 10: Lab 6 Lisp Programming and Working With Lisp Studio

The function can be executed at the terminal prompt:

Page 11: Lab 6 Lisp Programming and Working With Lisp Studio

Some Complex LISP Codes

(1) LISP code to return a given list as a list of atom pairs if it has even

number of atoms and returns nil if it has odd number of atoms.

(defun pair-off(lst)

(cond ((oddp (length lst))nil)

(t(cond ((null lst)nil)

(t(append (list (list (car lst) (car(cdr lst)))) (pair-off (cdr(cdr lst))))

)

)

)

)

)

(2) LISP code to reverse a given list upto first level only.

(defun myreverse (lst)

(cond ((null lst) nil)

( t (append (myreverse (cdr lst)) (list (car lst))))

)

)

(3) LISP code to count the number of atoms in a given list

(defun count-list (lst)

(cond ( (null lst) 0)

( (listp(car lst)) (+ 1 (count-list(cdr lst))) )

( t (count-list(cdr lst)))

)

)

Page 12: Lab 6 Lisp Programming and Working With Lisp Studio

(4) LISP code to duplicate a given list

(defun dup(lst)

(cond ((null lst) nil)

(t (append (mapcar #'list (list(car lst)) (list(car lst))) (dup(cdr lst))))

)

)

(5) LISP code to output the mirror of a given list

(defun mirror (lst)

(cond ((null lst)nil)

((atom lst)(list lst))

(t (cond ((listp(car lst)) (append (mirror(cdr lst)) (list (mirror (car lst)))))

(t (append(mirror(cdr lst))(list(car lst))))

)

)

)

)

Exercises:

1. Develop program to solve Fibonacci sequence till first 50 terms.

2. Develop program for Towers of Hanoi for four disks and three pegs.

Save yours works as AI_Lab6_IDNO_P.lisp where IDNo to be replaced by your

IDNO and P to be replaced by Problem number 1 or 2 as the case may be. Mail your

programs at [email protected]