Professional Documents
Culture Documents
Ai Lab
Ai Lab
TECHNOLOGY
CODE:
(defun add (num1 num2)
(+ num1 num2))
(defun subtract (num1 num2)
(- num1 num2))
(defun multiply (num1 num2)
(* num1 num2))
(defun divide (num1 num2)
(if (zerop num2)
(error "Division by zero is not allowed.")
(/ num1 num2)))
(let ((a 10)
(b 5))
(format t "Addition: ~a~%" (add a b))
(format t "Subtraction: ~a~%" (subtract a b))
(format t "Multiplication: ~a~%" (multiply a b))
(format t "Division: ~a~%" (divide a b)))
CODE:
(defun compare-three-numbers (num1 num2 num3)
(cond
((and (> num1 num2) (> num1 num3)) (format t "~a is the greatest.~%"
num1)) ((and (> num2 num1) (> num2 num3)) (format t "~a is the greatest.~
%" num2)) ((and (> num3 num1) (> num3 num2)) (format t "~a is the
greatest.~%" num3)) (t (format t "All three numbers are equal.~%"))))
(compare-three-numbers 5 9 3)
Q3) WAP to print table of accepted number
CODE:
(defun print-table (num)
(format t "Table of ~a:~%"
num) (dotimes (i 10)
(format t "~a * ~a = ~a~%" num (1+ i) (* num (1+ i)))))
(let ((number-to-print 7))
(print-table number-to-print))
CODE:
(defun armstrong-number-p (num)
(let ((num-str (format nil "~a" num))
(num-length (length (format nil "~a" num)))
(sum 0))
(dolist (digit (coerce num-str 'list))
(setq sum (+ sum (expt (parse-integer (string digit)) num-length))))
(= sum num)))
(let ((number-to-check 153))
(if (armstrong-number-p number-to-check)
(format t "~a is an Armstrong number.~%" number-to-check)
(format t "~a is not an Armstrong number.~%" number-to-
check)))
Q5) Write a LISP function to compute the sum of squares.
CODE:
(defun sum-of-squares (lst)
(if (null lst)
0
(+ (* (car lst) (car lst))
(sum-of-squares (cdr lst)))))
(let ((my-list '(1 2 3 4 5)))
(format t "The sum of squares is: ~a~%" (sum-of-squares my-list)))
Q6) Write a LISP function to compute the difference of squares.(if x > y return x2 – y2,
Otherwise y2 – x2).
CODE:
(defun difference-of-squares (x y)
(if (> x y)
(- (expt x 2) (expt y 2))
(- (expt y 2) (expt x 2))))
(let ((result (difference-of-squares 5 3)))
(format t "The difference of squares is: ~a~%" result))
Q7) Write a Recursive LISP function which takes one argument as a list and returns the last
element of the list. (Do not use the last predicate.)
CODE:
(defun last-element (lst)
(if (null lst)
nil
(if (null (cdr lst))
(car lst)
(last-element (cdr lst)))))
(let ((my-list '(1 2 3 4 5)))
(format t "The last element is: ~a~%" (last-element my-list)))
Q8) Write a Recursive LISP function which takes one argument as a list and return list
except the last element of the list. (Do not use butlast.)
CODE:
(defun list-except-last (lst)
(if (null lst)
nil
(if (null (cdr lst))
nil
(cons (car lst) (list-except-last (cdr lst))))))
(let ((my-list '(1 2 3 4 5)))
(format t "List except the last element: ~a~%" (list-except-last my-list)))
Q9) Write a Recursive LISP function which takes two arguments first an atom second a list
returns a list after removing the first occurrence of that atom within the list.
CODE:
(defun remove-first-occurrence (atom lst)
(if (null lst)
nil
(if (eql (car lst) atom)
(cdr lst)
(cons (car lst) (remove-first-occurrence atom (cdr lst))))))
(let ((my-list '(1 2 3 4 2 5)))
(format t "List after removing first occurrence of 2: ~a~%" (remove-first-occurrence 2 my-list)))
Q10) Write a Recursive LISP function which appends two lists together.
CODE:
(defun my-append (list1 list2)
(if (null list1)
list2
(cons (car list1) (my-append (cdr list1) list2))))
(let ((list1 '(1 2 3)))
(let ((list2 '(4 5 6)))
(format t "Appended List: ~a~%" (my-append list1 list2))))
Q12) Write a program in LISP to show family relations and facts apply queries on the given KB
CODE:
(defparameter *family-relations*
'((parent john mike)
(parent john lisa)
(parent mary mike)
(parent mary lisa)
(parent mike david)
(parent lisa emma)))
(defun mother (mother child)
(member (list mother child) *family-relations*))
(defun father (father child)
(member (list father child) *family-relations*))
(defun sibling (sibling1 sibling2)
(and (not (eq sibling1 sibling2))
(let ((parent1 (find-parent sibling1))
(parent2 (find-parent sibling2)))
(and parent1 parent2 (eq parent1
parent2))))) (defun find-parent (child)
(cdr (assoc child *family-relations*)))
(defun grandparent (grandparent grandchild)
(let ((parent (find-parent grandchild)))
(and parent
(let ((grandparent-of-parent (find-parent parent)))
(eq grandparent grandparent-of-parent)))))
(format t "Is John the father of Mike? ~a~%" (father 'john 'mike))
(format t "Is Lisa a sibling of Mike? ~a~%" (sibling 'lisa 'mike))
(format t "Is Mary a grandparent of Emma? ~a~%" (grandparent 'mary 'emma))
Q15) Write a program in LISP language to generate steps to solve Tower of Hanoi problem.
CODE:
(defun hanoi (n source auxiliary destination)
"Recursive function to solve Tower of Hanoi
problem" (if (= n 1)
(format t "Move disk from ~a to ~a~%" source destination)
(progn
(hanoi (- n 1) source destination auxiliary)
(format t "Move disk from ~a to ~a~%" source destination)
(hanoi (- n 1) auxiliary source destination))))
(hanoi 3 'A 'B 'C)