Lab Manual
EXPERIMENT
NO: NAME OF EXPERIMENT
1 Write a program showing use of LISP arithmetic functions.
2 Write a program for implementation of LISP manipulation functions.
3 Write a program to compute the average of three numbers.
4 Write a program to find largest of three numbers.
5 Write a program to find the area of circle.
6 Write a LISP program to implement factorial of a number without recursion
and with recursion.
7 Write a LISP program for find cube of the element of the list.
8 Write a program in LISP for Tower of Hanoi.
9 Write a LISP program to remove a property value.
10 Write a program in LISP that create an array and access it contents.
11 Write a program in LISP that stores items in array.
12 Write a program in LISP showing the use of MAPCAR function in LISP.
13 Write a program in PROLOG showing the use of predicate facts and rules.
14 Write a program in PROLOG to illustrate how domains can be used.
15 Write a program in PROLOG, which contains facts about the names and ages of
some of the pupils in a class.
16 Write a program in PROLOG, which contains facts about the names and ages of
some of the pupils in a class.
17 Write a program in PROLOG, which is a family relationships database that has
been heavily commented.
EXPERIMENT: 1
Objective: Write a program showing use of LISP arithmetic functions.
Function call value returned
(+ 3 5 8 4) 20
(- 10 12) -2
(* 2 3 4) 24
(/ 25 2) 12.5
EXPERIMENT: 2
Objective: - Write a program for implementation of LISP manipulation functions.
Function call value returned
(Car ‘(a b c)) A
(Cdr ‘(a b c)) (B C)
(Cons ‘a ‘(b c)) (A B C)
(List ‘a ‘(b c)) (A (B C))
(Append ‘(a) ‘(b c)) (A B C)
(Last ‘(a b c d)) (D)
(Member ‘b ‘(a b d)) (B D)
(Reverse ‘(a (b c) d)) (D (B C) A)
EXPERIMENT NO: 3
Objective: - Write a program for implementation of LISP Boolean function.
Function call value returned
(Atom ‘aabb) t
(Equal ‘a (car ‘(a b)) t
(Evenp 3) nil
(Number 10ab) nil(galat aa ra)
(Oddp 3) t
(Zerop .000001) nil
(Greater 2 4 27) t
(Lessp 5 3 1 2) nil
(Listp ‘(a)) t
(Null nil) t
Experiment No: 4
Objective: - Write a LISP Program calculating average of three numbers.
AVERAGE OF THREE NUMBERS
(defun averagethree(n1 n2 n3) (/(+ n1 n2 n3)3))
AVERAGETHREE
OUTPUT
(Averagethree 10 20 30)
20
EXPERIMENT NO: 5
Objective: - Write a program in LISP showing implementation of condition.
Maximum of two numbers
:->( defun maximum2 (a b) (cond (( > a b) a) (t b)))
MAXIMUM2
OUTPUT
(maximum2 234 320)
:-> 320
Maximum of three numbers
( defun maximum3(a b c)(cond(( > a b)(cond(( > a c)a)(t c)))( ( > b c)b)(t c)))
MAXIMUM3
OUTPUT
( maximum3 20 30 25)
:-> 30
EXPERIMENT: 6
Objective:- Write a LISP program to implement factorial of a number.
FACTORIAL OF A NUMBER
:-> (defun factorial (n)
cond((zerop n )1)
(t( * n(factorial ( - n 1)))))
FACTORIAL
:-> (factorial 6)
720
EXPERIMENT NO: 7
Objective: - Write a LISP program for declaring property list and assigning values to them.
(defun putprop(object value property)
(setf(get object property)value))
PUTPROP
TO ASSIGN PROPERTY
->(putprop ‘car ‘ford ‘make)
FORD
->(putprop ‘car 1988 ‘ year)
1988
->(putprop ‘car ‘red ‘colour)
RED
EXPERIMENT NO: 8
Objective: -. Write a LISP program to retrieve a property value.
(defun putprop(object value property)
(setf(get object property)value))
PUTPROP
->(putprop ‘car ‘ford ‘make)
FORD
->(putprop ‘car 1988 ‘ year)
1988
->(putprop ‘car ‘red ‘colour)
RED
RETRIEVE A PROPERTY
(get ‘car ‘colour)
RED
> (get ‘car ‘make)
FORD
EXPERIMENT NO: 9
Objective: -. Write a LISP program to remove a property value.
defun putprop(object value property)
(setf(get object property)value))
PUTPROP
->(putprop ‘car ‘ford ‘make)
FORD
->(putprop ‘car 1988 ‘ year)
1988
->(putprop ‘car ‘red ‘colour)
RED
Removing a property value
->(Remprop ‘car ‘colour)
RED
->(get ‘car ‘colour)
NIL
EXPERIMENT: 10
Objective:-Write a program in LISP that create an array and access it contents.
Theory:- Single or multidimensional arrays may be defined in LISP using the make-array
function .The items stored in array may be any LISP object. For example ,to create an array with
ten cells named myarray we bind the unquoted name to an array using setf(or setq) with the
make array function and specification of the number of cells.
(setf myarray(make –array ‘(10)))
#A(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL )
Note that the function returns the pound sign(#) followed by an A and the array representation
withits cells initially set to nil.
To access the contents of cells,we use the function aref which takes two arguments, the name of
the array and the index value.since the cells are indexed starting at zero ,an index value of 9 must
be used to retrieve the contents of the tenth cell.
->(aref myarray 9)
NIL
EXPERIMENT: 11
Objective:-Write a program in LISP that stores items in array.
Theory:-
To store items in the arrays,we use the function setf as to store properties on a property list .So to
store the items 25,red,and (sam sue linda) in the first,second,and the third cells of myarray.
:->( setf (aref myarray 0)25)
25
:->(setf (aref myarray 1) ‘red)
RED
:->(setf (aref myarray 2)’(sam sue linda))
(SAM SUE LINDA)
EXPERIMENT: 12
Objective:-Write use of MAPCAR function in LISP
Theory:- Mapping is one of the several mapping functions provided in LISP to apply some
function successively to one or more lists of the elements. The first argument of mapcar is a
function; the remaining argument(s) are lists of elements to which the named function is applied.
The results of applying the function to successive members of the lists are placed in a new list
which is returned .
For eg:-
Add 1 to each element of the list (5 10 15 20 25)
This can be done by adding the function 1+
:->(mapcar ‘1+ ‘(5 10 15 20 25))
(6 11 16 21 26)
:->
If we wish to add the corresponding elements of two lists(even of unequal length),use the +
function with the list to obtain the sum of the first four elements.
:->(mapcar ‘+ ‘(1 2 3 4 5 6) ‘(1 2 3 4))
:->(2 4 6 8)
EXPERIMENT: 13
Objective:-Write a program in PROLOG showing the use of predicate facts and rules.
domains
person, activity = symbol
predicates
likes(person, activity)
clauses
likes(rakesh, tennis).
likes(vivek, football).
likes(rupali, baseball).
likes(rohan, swimming).
likes(shruti, tennis).
likes(akash, X) if likes(rupali, X).
Type the above program into your computer and Run it. When the system responds in the dialog
window.
Goal :
likes(akash, baseball).
Turbo Prolog replies
True
Now enter the new goal
likes(akash, tennis).
Turbo prolog replies:
False
Now type the goal
likes(Individual, tennis).
Turbo Prolog replies
Individual = rakesh
Individual = shurti
2 Solutions
EXPERIMENT: 14
Objective:-Write a program in PROLOG to illustrate how domains can be used.
domains
brand, color = symbol
age, price = integer
mileage = real
predicates
car(brand, mileage, age, color, price)
clauses
car(audi, 130000, 3, red, 12000).
car(ford, 90000, 4, gray, 25000).
car(honda, 8000,1,red, 30000).
Goal:
car(_,_,Age,_,Cost) and Cost < 25000
Turbo Prolog replies
Age = 3, Cost = 12000
Age = 4, Cost = 25000
2 Solutions
EXPERIMENT: 15
Objective:-Write a program in PROLOG, which contains facts about the names and ages
of some of the pupils in a class.
domains
child = symbol
age = integer
predicates
pupil(child, age)
clauses
pupil(akansha, 9).
pupil(arjun, 10).
pupil(amit, 9).
pupil(susan, 9).
Goal:
pupil(Person1, 9) and
pupil(Person2, 9) and
Person1 <> Person2.
Turbo Prolog replies:
Person1=akansha, Person2=amit
Person1=akansha, Person2=susan
Person1=amit, Person2=akansha
Person1=amit, Person2=susan
Person1=susan, Person2=akansha
Person1=susan, Person2=amit
6 Solutions
EXPERIMENT: 16
Objective:-Write a program in PROLOG, which contains facts about the names and ages
of some of the pupils in a class.
domains
person = symbol
predicates
male(person)
smoker(person)
vegetarian(person)
ritika_could_marry(person)
goal
ritika_could_marry(X) and
write("a possible person for ritika is ",X) and nl.
clauses
male(nitin).
male(karan).
male(raju).
smoker(ramesh).
smoker(raju).
vegetarian(nitin).
vegetarian(raju).
ritika_could_marry(X) if male(X) and not(smoker(X)).
ritika_could_marry(X) if male(X) and vegetarian(X).
Turbo Prolog displays:
a possible person for ritika is nitin
EXPERIMENT: 17
Objective:-Write a program in PROLOG, which is a family relationships database that has
been heavily commented.
domains
person = symbol
predicates
male(person)
female(person)
father(person, person)
mother(person, person)
parent(person, person)
sister(person, person)
brother(person, person)
uncle(person, person)
grandfather(person, person)
clauses
male(salmaan).
male(shahrukh).
male(amir).
male(ajay).
female(anuskha).
female(vidya).
female(katrina).
female(rekha).
mother(katrina, anuskha).
mother(salmaan, rekha).
father(salmaan, amir).
father(anuskha, shahrukh).
father(vidya, amir).
father(katrina, salmaan).
parent(X, Y) if mother (X, Y).
parent(X,Y) if father(X,Y).
brother(X,Y) if
male(Y) and
parent(X,P) and
parent(Y,P) and
X<> Y.
sister(X,Y) if
female(Y) and
parent(X,P) and
parent(Y,P) and
X<> Y.
uncle(X,U) if
mother(X,P) and
brother(P, U).
uncle(X, U) if
father(X,P) and
brother(P,U).
grandfather(X,G) if
father(P,G) and
mother(X,P).
grandfather(X,G) if
father(X,P) and
father(P,G).