Академический Документы
Профессиональный Документы
Культура Документы
AIM:
Write a program in LISP to calculate the factorial of a number.
OBJECTIVE:
To know the logic behind the common Lisp loop for calculating factorial.
CODE:
->(defun factorial (n)
(cond ((zerop n) 1)
(t (* n(factorial (-n 1))))))
FACTORIAL
->
Contrast with an iterative version which uses Common Lisp's loop macro:
(defun factorial (n)
(loop for i from 1 to n
for fac = 1 then (* fac i)
finally (return fac)))
OUTPUT:
->(factorial 6)
720
->
PROGRAM - 2
AIM:
Write a program in LISP for Pythagorean Theorem.
OBJECTIVE:
To understand, how to perform various mathematical formula calculations.
LOGIC:
Tha Pythagorean Theorem states that the hypotenuse c of a right angle
triangle with short sides a and b is given by c=(a2+b2)1/2
(defun pythag (a b c)
(cond ((equal c 0) (sqrt (+ (expt a 2) (expt b 2))))
((equal b 0) (sqrt (- (expt c 2) (expt a 2))))
((equal a 0) (sqrt (- (expt c 2) (expt b 2))))
(t '(must have one unknown value.))))
CODE:
->(define path a b)
(sqrt (+ (* a a)(* b )))
OUTPUT:
->(path 3 4)
5
->
PROGRAM - 3
AIM:
Write a program in LISP to convert Fahrenheit to Celsius temperatures.
OBJECTIVE:
To understand the basics of various conversions.
LOGIC:
The standard conversion equation from Fahrenheit temperature F to Celsius
temperature oC is given by:
o
C = (5/9)*(F-32)
CODE:
->define(F-> C F)
(/(*5(-F 32)) 9))
OUTPUT:
-> (F ->C 212)
100
->
PROGRAM - 4
AIM:
Write a program in LISP to calculate area of a circle.
OBJECTIVE:
To know how to use various Lisp statements.
CODE:
->(defun circle-area ()
(terpri)
(princ Please enter the radius: )
(setq radius (read))
(princ The are of the circle is :)
(princ (* 3.1416 radius radius))
(terpri))
CIRCLE-AREA
->
OUTPUT:
->(circle -area)
Please enter the radius: 4
The area of the circle is: 50.2656
->
PROGRAM -5
AIM:
Write a program in LISP for Fibonacci sequence.
OBJECTIVE:
To use the various alternative methods of Lisp for making a sequence.
CODE:
<<printfib>>=
(defun printfib (start end)
(format t "(fib-trec ~D)=~D~%" start (fib-trec start))
(if (< start end)
(printfib (+ start 1) end)))
Alternative way:
->(define ( Fibonacci n)
(cond ((=? n 0) 0)
((=? n 1) 1)
(else (+ ( fibonacci (-n 1))
( fibonacci (-n 2))))))
OUTPUT:
When calling printfib, the output should be:
(fib-trec 1)=1
(fib-trec 2)=1
(fib-trec 3)=2
(fib-trec 4)=3
(fib-trec 5)=5
PROGRAM -6
AIM:
Write a program in LISP to find the maximum of three numbers.
OBJECTIVE:
After this, one can understand the various methods of comparison.
CODE:
->(defun maximum3 (a b c)
(cond ((> a b) (cond ((> a c) a)
(t c)))
((>b c) b)
(t c)))
->MAXIMUM3
OUTPUT:
->(MAXIMUM3 11 27 15)
30
->
PROGRAM -7
AIM:
Write a program in LISP to calculate the average.
OBJECTIVE:
To understand , how to find out the average value using Lisp syntax.
LOGIC:
The format is:
(defun name (parm1 parm2.) body)
CODE:
->(defun averagethree (n1 n2 n3)
(/ (+ n1 n2 n3) 3))
AVERAGETHREE
->
OUTPUT:
->(averagethree 10 20 30)
20
->
PROGRAM -8
AIM:
Write a program in LISP for Water Jug Problem.
OBJECTIVE:
To find the solution of various gaming problems.
CODE:
(defvar *start* '(0 0))
(defun first-jug (state) (car state))
(defun second-jug (state) (cadr state))
(defun mk-state (f s) (list f s))
(defun goalp (state)
(eq (first-jug state) 2))
(defun new-states (state)
(remove-null
(list
(fill-first state)
(fill-second state)
(pour-first-second state)
(pour-second-first state)
(empty-first state)
(empty-second state))))
(defun remove-null (x)
(cond
((null x) nil)
((null (car x)) (remove-null (cdr x)))
((cons (car x) (remove-null (cdr x))))))
PROGRAM -9
AIM:
Write a program in LISP for Depth First Search.
OBJECTIVE:
To understand the logic of DFS using different functions.
CODE:
(defun dfs (state depth limit)
(setf *nodes* 0)
(setf *expanded* 0)
(setf *branches* 0)
(setf *limit* limit)
(setf *result* (dfs1 state depth))
(print (list *nodes* *expanded* *branches*))
*result*
)
;;; dfs1 expands a node and calls dfs2 to recurse on it
(defun dfs1 (state depth)
(setf *nodes* (+ 1 *nodes*))
(cond
((goalp state) (list state))
((zerop depth) nil)
((> *nodes* *limit*) nil)
((let ((children (new-states state)))
(setf *expanded* (+ 1 *expanded*))
(setf *branches* (+ (length children) *branches*))
(let ((result (dfs2 children (- depth 1))))
(and result (cons state result)))))))
;;; dfs2 recurses on each sibling from a single node, calling dfs1
(defun dfs2 (states depth)
(cond
((null states) nil)
((dfs1 (car states) depth))
((dfs2 (cdr states) depth))))
OUTPUT:
> (dfs *start* 7 100000)
(584 206 591)
; Branching factor 2.86 (591/206)
((0 0) (4 0) (1 3) (1 0) (0 1) (4 1) (2 3))
PROGRAM -10
AIM:
Write a program in LISP for Breadth First Search.
OBJECTIVE:
To understand the usage of BFS using Lisp.
CODE:
(defun bfs (state limit)
(setf *nodes* 0)
(setf *expanded* 0)
(setf *branches* 0)
(setf *limit* limit)
(setf *result* (bfs1 (list (list state))))
(print (list *nodes* *expanded* *branches*))
(reverse *result*))
(defun bfs1 (queue)
(setf *nodes* (+ 1 *nodes*))
(cond
((null queue) nil)
((goalp (caar queue)) (car queue))
((> *nodes* *limit*) nil)
((let ((children (new-states (caar queue))))
(setf *expanded* (+ 1 *expanded*))
(setf *branches* (+ (length children) *branches*))
(bfs1
(append
(cdr queue)
(mapcar
#'(lambda (state)
(cons state (car queue)))
children)))))))
OUTPUT:
> (bfs *start* 100000)
(341 340 981)
; Branching factor 2.88 (981/340)
((0 0) (4 0) (1 3) (1 0) (0 1) (4 1) (2 3))
PROGRAM -1
AIM:
Write a program in LISP to define structure types for area of triangle.
OBJECTIVE:
To use the structure in Lisp.
CODE:
(defstruct triangle
(base ())
(altitude () ))
(defun triangle-area (figure)
(*1/2
(triangle-base figure)
(triangle-altitude figure)))
OUTPUT:
->(area triangle 2 3)
3
->
PROGRAM -2
AIM:
Write a program in LISP to define structure types for area of rectangle.
OBJECTIVE:
To understand the logic of Lisp structure by finding the area of rectangle.
CODE:
(defstruct rectangle
(width ())
(height () ))
(defun rectangle-area (figure)
(* (rectangle-width figure)
(rectangle-height figure)))
OUTPUT:
->(area-rectangle 5 7)
35
->
PROGRAM -3
AIM:
Write a program in LISP to define structure types for area of circle.
OBJECTIVE:
To know how the various symbols and exponential values are used.
CODE:
(defstruct circle
(radius () ))
(defun circle-area (figure)
(*pi (expt (circle-radius figure ) 2)))
OUTPUT:
->(averagecircle 11)
380.13
->
PROGRAM -4
AIM:
Write a program in LISP to define methods through certain planar figures.
OBJECTIVE:
To know the use of various methods in a program.
CODE:
(defmethod area ((figure triangle))
(*1/2
(triangle-base figure)
(triangle-altitude figure)))
(defmethod area ((figure rectangle))
OUTPUT:
->(area triangle);
Matches triangle method.
3
->
->(area rectangle);
Matches rectangle method.
35
->
->(area circle);
Matches circle method.
380.13
->
PROGRAM -5
AIM:
Write a program in LISP for BUBBLE SORT.
OBJECTIVE:
To know , how the combination of loops work in a program.
LOGIC:
(defun bubble-sort (array cmp-fun)
"Bubble sort implementation in common lisp. Using the extended loop
facility."
(let ((result (copy-seq array)))
(loop for i from (1- (length result)) downto 0 do
(loop for j from 0 to i
when (funcall cmp-fun (aref result i) (aref result j))
do (rotatef (aref result i) (aref result j)) ))
result))
CODE:
defun bubble-sort-vector (vector predicate &aux (len (1- (length vector))))
(do ((swapped t)) ((not swapped) vector)
(setf swapped nil)
(do ((i (min 0 len) (1+ i))) ((eql i len))
(when (funcall predicate (aref vector (1+ i)) (aref vector i))
(rotatef (aref vector i) (aref vector (1+ i)))
(setf swapped t)))))
OUTPUT:
->(BUBBLE SORT 27 11 15 38 1)
1 11 15 27 38
->
rectangle.
3. Write a program in LISP to define structure types for area of circle.
4. Write a program in LISP to define methods through certain planar
figures.
5. Write a program in LISP for BUBBLE SORT.