clisp lab manual - dr.j.vijipriya
TRANSCRIPT
![Page 1: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/1.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 1
Page
1Ex.No : 1 Area of Circle (3.14*R*R) Using Function
Aim:To write CLISP code to find the Area of Circle using function
Procedure:1. Define function AreaOfCircle for computing area of circle2. Read radius ( r ) of Circle 3. Define constant value PI=3.144. Compute Area of Circle using Area=PI*r*r5. Print Area of Circle
Program:(defconstant PI 3.14)(defun AreaOfCircle()
(terpri)(print "Enter Radius of Circle:")(setq r (read))(terpri)(format t "Area of circle = ~d" (* PI r r))
)(AreaOfCircle)
Note:;;defconstant-> used to declare constant;; defun -> used to define function;; AreaOfCircle() -> function name is defined by user,followed by paranthesis();;format-> producing well formatted text-> (format destination contril-string argument);; destination (t) -> standaed output;; control string("Area of circle = ~d") ->holds the characters to be output and printing directives;; ~d->one of the printing directives (~d for decimal arguments,~a for ASCII arguments,;;~c for character arguments, ~f for floating point arguments,~% for new line);;(AreaOfCircle)-> Function calling
Output:
Enter Radius of Circle: 5Area of circle = 78.5
Result :
The program for Area of Circle was executed and verified successfully
![Page 2: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/2.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 2
Page
2Ex.No : 2 Arithmetic Operation using Standard I/O Functions
Aim:To write CLISP code to perform arithmetic operation using using standard I/O Functions (read write and print)
Procedure:1. Read the value of x and y2. Compute the addition, subtraction, multiplication and Division3. Print the results of addition, subtraction, multiplication and Division
Program:(print " Enter value of x:")(setq x (read))(print " Enter value of y:")(setq y (read))(write (+ x y)) (terpri)(write (- x y))(terpri)(write (* x y))(terpri)(write (/ x y))
Note:;; -> Comment line -> compiler or interpreter ignore this line (ie this line will not be executed by the compiler);; read -> Reading characters from keyboard and interpret them as LISP objects -> (read);; write /print -> Print the object with a preceding newline and follewd by a space ;; setq -> Assign the value to the variable ;;(+ x y)-> LISP uses prefix notaion. In prefix notation, Operators are written before their operands;; (terpri)->It outputs a newline to output-stream
Output:Enter the value of x: 10Enter the value of y: 2128205Result:The program for arithmetic operation was executed and verified successfully
![Page 3: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/3.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 3
Page
3Ex.No : 3 Find Two Numbers are equal using IF-THEN-ELSE
Aim:To write CLISP code to find two numbers are equal using IF-THEN-ELSE
Procedure:1. Read the value of two numbers a and b2. Find a and b are equal using if–then-else3. Print the result whether both are equal or not
Program:(print " Enter value of a:")(setq a (read))
(print " Enter value of b:")(setq b (read))
(if (equal a b)(format t "~% both ~a and ~a are equal" a b)(format t "~% both a=~a and b=~a are not eqaul" a b))
Note:;; (if (test-clause) (action1) (action2));; if test-clause is t(true),action1 is executed; else action2 is executed
Output:
Enter the value of a: 5Enter the value of b: 3Both 5 and 3 are not equal
Enter the value of a: 25Enter the value of b: 25Both 25 and 25 are equal
Result:The program for find given two numbers are equal was executed and verified successfully
![Page 4: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/4.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 4
Page
4Ex.No : 4 Compare Two Numbers using cond
Aim:To write CLISP code to compare two numbers using cond
Procedure:1. Read the value of a and b2. Compare a and b using cond3. Print the result whether a and b are equal or a is smaller than b or a is bigger than b
Program:(print " Enter value of a:")(setq a (read))(print " Enter value of b:")(setq b (read))(cond ((equal a b)(format t "~% both ~a and ~a are equal" a b))
((< a b) (format t "~% ~a is smaller than ~a " a b))((> a b)(format t "~% ~a is greater than ~a " a b))
Note:;;(cond (test1 action1)(test2 action2)...(testn actionn))
;;Each clause within the cond statement consists of a conditional test;;and an action to be performed.
;;If the first test following the cond, i.e. test1, is evaluated to be true,;;then the corresponding action part, i.e. action1 is executed.;;Further, its value is returned and the rest of the clauses are skipped.
;;If test1 evaluates to be nil,;;then program control moves to the second clause without executing action1,;;and the same process is followed.
;;If none of the test conditions are evaluated to be true,;;then the cond statement returns nil.
Output:Enter the value of a: 5, Enter the value of b: 5, 5 and 5 are equalEnter the value of a: 10, Enter the value of b: 30, 5 is smaller than 30Enter the value of a: 90, Enter the value of b: 43, 90 is greater than 30
Result:The program for compare given two numbers was executed and verified successfully
![Page 5: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/5.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 5
Page
5Ex.No : 5 Find day of given number using case
Aim:To write CLISP code to find the day of given numbers using caseProcedure:
1. Read the number of the day2. Print the correspond day (Sunday or… or Saturday) of the given number using case
Program:(print "Enter the no. of day")(setq day (read))
(case day(1 (format t "~% Sunday"))(2 (format t "~% Monday"))(3 (format t "~% Tuesday"))(4 (format t "~% Wednesday"))(5 (format t "~% Thursday"))(6 (format t "~% Friday"))(7 (format t "~% Saturday"))
)
Note:;;(case keyform;;(key1 (action1 action2 ...) );;(key2 (action1 action2 ...) );;...;;(keyn (action1 action2 ...) ))
;;The case construct implements multiple test-action clauses;;such as the cond construct.;;However, it evaluates a key form;;and allows multiple action clauses based on the evaluation of that key form
Output:Enter the no.of day:5Thursday
Result:The program for find the day of given number using case was executed and verified successfully
![Page 6: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/6.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 6
Page
6Ex.No : 6 Print 1 to 20 using loop
Aim:To write CLISP code to print 1 to 20 using loop
Procedure:1. Set the value of n as zero2. Increment the value of n by 1 using loop3. Print the value of n 4. Repeat steps 2 and 3 until n>= 20
Program:(setq n 0)(loop (setq n (+ n 1))
(write n)(terpri)(when (>= n 20) (return n))
)
Note:;;The loop construct execute some statement(s) repeatedly until it finds a return statement;;(loop (s-expressions))
Output:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Result:The program for print 1 to 20 using loop was executed and verified successfully
![Page 7: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/7.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 7
Page
7Ex.No : 7 Print Even Numbers in given list using loop for
Aim:To write CLISP code to print even numbers in given list using loop for
Procedure:1. Read element of the list (1…20) using loop for2. Check every element of the list is even using evenp predefined function3. Print even numbers only
Program:(loop for x from 1 to 20
do (if (evenp x)(format t " ~a" x)
))
Note:;;It allows to set up variables for iteration ;;specify expression(s) that can conditionally terminate the iteration ;;specify expression(s) for performing some job on each iteration
;;(loop for loop-variable from value1 to value2;; do (action))
Output:2 4 6 8 10 12 14 16 18 20
Result:The program for find even numbers in given list using loop for was executed and verified successfully
![Page 8: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/8.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 8
Page
8Ex.No : 8 Find the average of given numbers using function with argument
Aim:To write CLISP code to find the average of given numbers using function with argument
Procedure:1. Read the no. of value (no) to be averaged2. Set variable sum is as 0 and define function average3. In that function, read the values (a) to be averaged using loop for and find sum=sum +(value) a4. find the average ( sum/no) and print average
Program:(print "Enter the no. of value to be averaged:")(setq no (read))(setq sum 0)(defun average(no)(loop for x from 1 to no
do(format t "Enter value ~a::" x)(setq a (read))(setq sum (+ sum a))
)(format t "Average =~a" (/ sum no))
) (average no)
Note:;; Find the average of given numbers using function with argument
;;The macro named defun is used for defining functions.;;The defun macro needs three arguments:;;1.Name of the function ;;2.Parameters of the function ;;3.Body of the function ;;(defun fun-name (parameter-list);;"Optional documentation string.";;body)
Output:Enter the no. of value to be averaged: 3Enter value: 5 6 7 Average =6
Result:The program for the average of given numbers using function was executed and verified successfully
![Page 9: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/9.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 9
Page
9Ex.No : 9 Sorting the List using ArrayAim:To write CLISP code to sorting the list using Array
Procedure:1. Read 5 elements of the list using array and loop for2. Print the list before sorting3. Sort all the elements in the given list in ascending order using two loops (inner and outer loop)4. Compare adjacent elements if A[i-1]> A[i],exchange the values of A[i-1] and A[i] using
temporary variable temp (swapping)5. Print the sorted list
Program:(setf array(make-array '(6)));define array(loop for x from 0 to 5
do (format t " Enter the value of A[~a]= " x)(setf (aref array x) (read)); get array values from keyboard
) (print "Before sorting")(loop for x from 0 to 5
do(format t " ~a" (aref array x));display all array values)
(print "After sorting")(loop for y from 0 to 5;outer loop ;y=0
do (loop for x from 1 to 5; inner loop;x=1 do (when (> (aref array (- x 1))(aref array x)); A[0]>A[1]4>2
(setf temp (aref array (- x 1)))(setf (aref array (- x 1)) (aref array x))(setf (aref array x) temp))
))(loop for x from 0 to 5
do(format t " ~a" (aref array x)))Output:Enter the value of A[0]= 94 , A[1]=66, A[2]= 78, A[3]=12, A[4]=13, A[5]=21Before sorting: 94 66 78 12 13 21 After sorting: 12 13 21 66 78 94
Result:The program for sorting the given list using array was executed and verified successfully
![Page 10: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/10.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 10
Page
10Ex.No : 10 Find the Volume of Box using class
Aim:To write CLISP code to find the volume of the box using classProcedure:
1. Read the length, breadth and height of the box in base class box() and define function vol to find the volume of the box (vol=len*bre*hei)
2. Define subclass (derived class) wooden() and read price of the box3. Create object of the subclass wooden to access all the members of the base class and subclass4. Print length, breadth and height, volume and price of the box
Program:(defclass box()( (len :accessor b-len)
(bre :accessor b-bre)(hei :accessor b-hei)(vol :reader vol)
))
(defmethod vol((B box))(* (b-len B) (b-bre B) (b-hei B)))(defclass wooden(box)((pri :accessor b-pri)))
(print “Enter the value of len,bre hei and price”) (setf item(make-instance 'wooden))(setf (b-len item) (read))(setf (b-bre item) (read))(setf (b-hei item) (read))(setf (b-pri item) (read))
(format t "~% Length=~a" (b-len item))(format t "~% Breadth=~a" (b-bre item))(format t "~% Height=~a" (b-hei item))(format t "~% Price=~a" (b-pri item))(format t "~% Volume=~a" (vol item))Output:Enter the value of len,bre hei and price: 5 2 3 68.7Length=5 Breadth=2 Height=3 price=68.7 Volume=30Result:The program for find the Volume of Box using class was executed and verified successfully
![Page 11: CLISP Lab Manual - Dr.J.VijiPriya](https://reader038.vdocuments.us/reader038/viewer/2022100801/5888a20f1a28ab264b8b611f/html5/thumbnails/11.jpg)
CLISP LAB MANUAL
Prepared by Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia Page 11
Page
11
Exercises1. Write CLISP code to print out all the prime numbers up to 10002. Write CLISP code for calculating square of an integer number, say 5, using function.3. Write CLISP code for calculating factorial of an integer number using function.4. Write CLISP code to print first 5 natural numbers using function5. Write CLISP code to calculate the average marks of 5 subjects, obtained by a student where marks
are present in an integer array6. Write CLISP code to print the contents of an integer array using function.7. Write CLISP code to find the greatest of three numbers?8. Write CLISP code to create a menu for arithmetic calculation like Sum, Difference, Product, Divide
and program should stop execution only if user give '0' as the choice9. Write CLISP code to print the multiplication table for the user input10. Write CLISP code to perform SUM, DIFFERENCE, PRODUCT of two values11. Write a program to print a value and increment it by 5