ailab_10103007
TRANSCRIPT
ARTIFICIAL INTELLIGENCELAB
Departmental Lab VII
Submitted By:
Ankit Vij10103007CSE , 4th year
1
INDEX
Sr. No. Program Page No.
1 Program 1 3
2 Program 2 4
3 Program 3 5
4 Program 4 6
5 Program 5 8
6 Program 6 9
7 Program 7 10
8 Program 8 11
9 Program 9 12
10 Program 10 13
11 Program 11 14
12 Program 12 15
2
PROGRAM 1
Write a LISP program to carry out basic arithmetic operations like addition, subtraction, multiplication and division on the set of 8 values.
Source Code:
(print "ANKIT VIJ's PROGRAM 1")Addition:
(+ 3 9 1 8 3 2 1 3)>> 30
Subtraction:(- 10 1 1 2 1 1 1 1) >> 2
Multiplication:(* 4 6 5 2 3 1 2 10)>> 14400
Division:(/ 1000 2 2 5 5 1 1 2)>> 5
Output
3
PROGRAM 2
Write a LISP program to find square root of a given number.
Source Code
(print "ANKIT VIJ's PROGRAM 2")Square root: (sqrt 144)
Output:>> 12.0
Output
4
PROGRAM 3
Write a LISP program to find the Greatest Common Divisor of a given number.
Source Code:
(print "ANKIT VIJ's PROGRAM 3")Greatest Common Divisor: (gcd 2 3 5)
Output:>> 1
Output:
5
PROGRAM 4
Write a LISP program to carry out various Trigonometric Functions.
Source Code:Trigonometric functions-
(sin (/ pi 2))
Output:>>1.0
(cos pi)
Output:>>-1.0
(tan (/ pi 4))
Output:>>0.9999999
Output
Source Code
6
Inverse trigonometric functions- (* 180 (/ 1 pi) (asin 1))
Output:>>90.0
(* 180 (/ 1 pi) (acos 1))
Output:>>0.0
(* 180 (/ 1 pi) (atan 1))
Output:>>45.0
Output
PROGRAM 5
7
Write a LISP program to find min and max of n numbers where the value of N is decided by the user at run time.
Source Code: (max 1 2 3 4 5)Output:>>5
(min 1 2 3 4 5)Output:>>1
Output
PROGRAM 6
8
Write a LISP program to find 5th maximum and 5th minimum of a given set of N numbers.
Source Code
5th minimum:
(nth 4 (sort '(5 8 9 6 7 2 1 6 5 8 9 4 7) #’<))
Output:>>5
5th maximum:
(nth 4 (sort '(5 8 9 6 7 2 1 6 5 8 9 4 7) #’>))
Output:>>7
Output
PROGRAM 7
9
Write a LISP program to create array, adding and deleting elements in the array of 10 elements
Source Code:
Single –Dimension array:Declaration: (defvar a)Definition: (setq a (make-array 10 :initial-contents ‘(1 2 3 4 5 6 7 8 9 10)))Referring to element with index: (aref a 1)Adding a new value to the array: (setf (aref a 1) 4)Delete element from an array: (delete 4 a)
Multi-dimension arrayDeclaration: (defvar a)Definition: (setq a (make-array ‘(2 2) :initial-contents ‘( (2 4) (5 6) ))) Referring to element with index: (aref a ‘(1 1))Adding a new value to the array: (setf (aref a ‘(1 1)) 4)Delete element from an array: (delete ‘(1 1)a)
Output
PROGRAM 8
10
Write a LISP program to illustrate the use of “prong”.
Progn: It is used to associate multiple actions on satisfaction of condition in if-else statements:
Source Code(if (> 5 3)
(progn (print "hello") (print "How are you") 9) )
Result:
“hello”“How are you”9
Output
PROGRAM 9
11
Write a LISP Program to find factorial of given number.
Source Code
Implementing using recursion-
factorial_Number.cl(defun factorial (n) (if (<= n 0) 1 (* n (factorial (- n 1)))))
Output
(print “ANKIT VIJ’s PROGRAM 9”)“ANKIT VIJ’s PROGRAM 9”“ANKIT VIJ’s PROGRAM 9”>(load ‘factorial_Number.cl)>(factorial 4) >>24
PRORGAM 10
Write a LISP program to find greater of two numbers.
12
Source Code:
newGreater.cl:
(defun greater (x y)(if (> x y)xy))
Output
(print “ANKIT VIJ’s PROGRAM 10”)“ANKIT VIJ’s PROGRAM 10”“ANKIT VIJ’s PROGRAM 10”>(load ‘newGreater.cl)>(greater 3 2)>>3
13
PROGRAM 11
Write a LISP program to find greatest of three numbers.
Source Code:
‘newGreatest.cl
(defun greatest(x y z)(max x y z))
Output
>(print “ANKIT VIJ’s PROGRAM 11”)“ANKIT VIJ’s PROGRAM 11”“ANKIT VIJ’s PROGRAM 11”>(load ‘newGreatest.cl)>(greatest 2 1 3)>>3
14
Q12. Write a LISP program to find:a. Breadth First Searchb. Depth First search
A12. a. Breadth First Search
(defun breadth_first (arr)(setq len (length arr))(setq z (- len 1))(loop for x from 0 to zdo(
if(= (aref arr x) -699)nil(print (aref arr x)))))
b. Depth First Search
(defun dfs (arr index)(setq len (length arr))(if (> index len) (return))
(if (= (aref arr index) -666)Nil(print (aref arr index)) )(setq left (+ (*2 index) 1 ))(setq rightt (+ (*2 index) 2 ))(dfs (arr left))
15
(dfs (arr right)))
16