Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

SHARDA SCHOOL OF ENGINEERING &

TECHNOLOGY

DEPARTMENT OF COMPUTER SCEINCE AND


APPLICATIONS

Course Name: ………………………………………

Course Code: ………………………………………

Program: …...………………. Batch: …………….

Year/Semester: ……………. Session: ……...……

Date of Submission of Assignment: ……………….

Submitted By: Submitted To:

Name: …………………………... …………………………….

Roll No: ………………………… …………………………….

Signature of Student : ……………


INDEX
TEACHER’S
S.No AIM DATE SIGNATURE
01 WAP to implement basic arithmetic operations

02 WAP to compare three numbers

03 WAP to print table of accepted number

04 WAP to check Armstrong number


05 Write a LISP function to compute the sum of squares.

06 Write a LISP function to compute the difference


of squares.(if x > y return x2 – y2, Otherwise y2
– x2).
07 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.)
08 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.)
09 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.
10 Write a Recursive LISP function which appends
two lists together.
11 Write a program in LISP language to implement:
i) BFS ii)DFS

12 Write a program in LISP to show family relations


and facts apply queries on the given KB
13 Write LISP program to Program to categorize
animal characteristics.

14 Write LISP program to solver for the linear equation


A*X + B = 0. Let the predicate linear (A, B, X) return
the root X of the equation.
15 Write a program in LISP language to generate
steps to solve Tower of Hanoi problem.
Q1) WAP to implement basic arithmetic operations

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)))

Q2) WAP to compare three numbers

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))

Q4) WAP to check Armstrong number

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))))

Q11) Write a program in LISP language to implement


i) BFS
CODE:
(defvar *graph*
'((a b)
(b a c d)
(c b e)
(d b)
(e c)))
(defun bfs (graph start)
"Breadth-First Search on a
graph." (let ((visited (list start))
(queue (list start)))
(loop while queue do
(let* ((current (pop queue))
(neighbors (cdr (assoc current graph))))
(dolist (neighbor neighbors)
(unless (member neighbor visited)
(format t "~a " neighbor)
(push neighbor visited)
(push neighbor queue)))))))
(format t "~%BFS: ")
(bfs *graph* 'a)
ii) DFS
CODE:
(defvar *graph*
'((a b)
(b a c d)
(c b e)
(d b)
(e c)))
(defun dfs (graph start visited)
"Depth-First Search on a
graph." (if (not (member start
visited))
(progn
(format t "~a " start)
(push start visited)
(dolist (neighbor (cdr (assoc start graph)))
(dfs graph neighbor visited)))))
(format t "DFS: ")
(dfs *graph* 'a '())

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))

Q13) Write LISP program to Program to categorize animal characteristics.


CODE:
(defun categorize-animal (animal)
(cond ((eq animal 'mammal) "This animal is warm-blooded.")
((eq animal 'bird) "This animal has feathers.")
((eq animal 'reptile) "This animal is cold-blooded.")
((eq animal 'amphibian)"This animal can live both in water and on land.")
((eq animal 'fish) "This animal lives in water.")
((eq animal 'invertebrate) "This animal lacks a backbone.")
(t "Unknown animal category.")))
(format t "Mammal: ~a~%" (categorize-animal 'mammal))
(format t "Bird: ~a~%" (categorize-animal 'bird))
(format t "Reptile: ~a~%" (categorize-animal 'reptile))
(format t "Fish: ~a~%" (categorize-animal 'fish))
(format t "Invertebrate: ~a~%" (categorize-animal 'invertebrate))
(format t "Alien: ~a~%" (categorize-animal 'alien))
Q14) Write LISP program to solver for the linear equation A*X + B = 0. Let the predicate
linear (A, B, X) return the root X of the equation.
CODE:
(defun linear (a b)
(if (zerop a)
(if (zerop b)
(format t "Any real number is a solution.")
(format t "No solution."))
(format t "The solution is: ~a" (/ (- 0 b) a))))
(linear 2 4)

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)

You might also like