0% found this document useful (0 votes)
43 views

LISP Programs 1. To Perform Primitive Operations (I.e CAR, CDR, APPEND)

The document contains examples of LISP programs that perform various operations on lists: 1. Perform primitive operations like CAR, CDR and APPEND on lists. 2. Square each element of a given list. 3. Check if a given element is present in a list. 4. Find the sum of numbers using recursion. 5. Perform stack operations like PUSH and POP. 6. Reverse a given list without using built-in functions. 7. Find the powerset of a given list. 8. Calculate the factorial of a given number. 9. Find the roots of a quadratic equation. 10. Count the number of elements in a given list.

Uploaded by

ShashikanthKurle
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

LISP Programs 1. To Perform Primitive Operations (I.e CAR, CDR, APPEND)

The document contains examples of LISP programs that perform various operations on lists: 1. Perform primitive operations like CAR, CDR and APPEND on lists. 2. Square each element of a given list. 3. Check if a given element is present in a list. 4. Find the sum of numbers using recursion. 5. Perform stack operations like PUSH and POP. 6. Reverse a given list without using built-in functions. 7. Find the powerset of a given list. 8. Calculate the factorial of a given number. 9. Find the roots of a quadratic equation. 10. Count the number of elements in a given list.

Uploaded by

ShashikanthKurle
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LISP Programs

1. To perform primitive operations (i.e CAR, CDR,APPEND)


(format t"~%Enter the list1 : ~%")
(setf list1(read))
(format t"~%Enter the list2 : ~%")
(setf list2(read))
(format t"~%~% The CAR of 1st list is : ~a" (car list1))
(format t"~%~% The CDR of 1st list is : ~a" (cdr list1))
(format t"~%~% The appended list is : ~a" (append list1 list2))

OUTPUT :
Enter the list1 : (1 2 3)
Enter the list2 : (4 5 6)
The CAR of 1st list is : 1
The CDR of 1st list is : (2 3)
The appended list is : (1 2 3 4 5 6)

2. To perform squaring of each element of a given list


(defun sqr(n)
(if (null n) nil
(cons(*(car n) (car n))
(sqr(cdr n)))))
(format t"~% Enter the list : ")
(setq list(read))
(format t"~% Square of each element of given list : ~a" (sqr list))
OUTPUT:
Enter the list : (2 3 4)
Square of each element of given list : (4 9 16)

3. To check whether the given element is present in the list or not


(defun check(Item Is)
(if(null Is) 0
(if(atom(car Is))
(if(equal Item(car Is)) 1
(check Item(cdr Is)))
(check Item(cdt Is)))))
(format t"~%~% Enter the list: ")
(setf Is(read))
(format t"~% The given list is : ~a" Is)
(format t"~% Enter element to be searched : ")
(setf Item(read))
(if(zerop(check Item Is))
(format t"~% Item is not present")
(format t"~% Item is present"))
OUTPUT:
Enter the list: (a 4 d f 6)
The given list is : (A 4 D F 6)
Enter element to be searched : d
Item is present

4. To find sum of N numbers using recursion


(defun sum(n)
(if(= n 0) 0
(+ n(sum (- n 1)))))
(format t "~% Enter the range : ")
(setf n(read))
(format t "~% Sum of given number is : ~a" (sum n))

OUTPUT:
Enter the range : 7
Sum of given number is : 28

5. To perform STACK operations


(defun menu()
(format t"~% press 1 to perform push operation")
(format t"~% press 2 to perform pop operation")
(format t"~% press 3 to quit")
(format t"~% Enter ur choice"))
(defun push()
(format t"~%stack before push:")
(princ stack)
(format t"~%Enter data to be pushed:")
(setq data(read))
(setq stack(append stack(list data)))
(format t"~%Stack after push operation")
(princ stack))
(defun pop()
(format t "~%Stack before pop:")
(princ stack)
(setq data(CAR (last stack)))
(princ data)
(setq stack(butlast stack))
(format t"~%Stack after pop operation ")
(princ stack))

(defun quit()
(format t"~%Exit"))
(format t"~%Enter initial")
(setq stack(read))
(menu)
(setq code(read)ch'y)
(do() ((not(EQL ch'y))nil)
(case code
(1 (push))
(2 (pop))
(3 (quit)))
(cond((= code 3)(setq ch 'n))
(t (format t"~% Do you want to continue(y/n):")
(setq ch(read))
(cond ((EQL ch'y)
(menu)
(setq code(read)))
(t (setq code 3))
))))
OUTPUT:

Enter initial4
press 1 to perform push operation
press 2 to perform pop operation
press 3 to quit

Enter ur choice1
stack before push:4
Enter data to be pushed:12
Stack after push operation(12)
Do you want to continue(y/n):y

press 1 to perform push operation


press 2 to perform pop operation
press 3 to quit
Enter ur choice2
Stack before pop:(12)12
Stack after pop operation NIL
Do you want to continue(y/n):n

6. To reverse a given list without using inbuilt function


(defun reversel(l)
(cond((null l)l)
(t(append(reversel(cdr l))
(cons(car l) nil)))))
(format t"~% Enter the List: ")
(setf l(read))
(format t"~% Reversed list is : ~a" (reversel l))

OUTPUT:
Enter the List: (2 3 4 5)
Reversed list is : (5 4 3 2)

7. To perform function powerset that takes a list l as input & returns the powerset of the list
(defun powerset(l)
(if (null l)
'(nil)
(let ((ps (powerset (cdr l))))
(append ps(mapcar #'(lambda (x) (cons (car l) x)) ps)))))
(format t"~% Enter the list : ")
(setf l(read))
(format t"~% Powerset : ~a" (powerset l))

OUTPUT:
Enter the list : (1 2 3)
Powerset : (NIL (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))

8. To find factorial of a given number.


(defun factorial(n)
(if(= n 0)1
(* n(factorial(- n 1)))))
(format t"~% Enter number : ")
(setf n(read))
(format t"Factorial = ~a" (factorial n))

OUTPUT:
Enter number : 5
Factorial = 120

9. To find the roots of a given quadratic equation.


(defun quadratic_roots(a b c)
(let((discriminant(-(* b b) (* 4 a c))))
(values(/(+(- b)(sqrt discriminant))(* 2 a))
(/(-(- b)(sqrt discriminant))(* 2 a)))))
(format t"~% Enter the numbers :~%")
(setf a(read))
(setf b(read))
(setf c(read))
(format t" Root = ~a " (quadratic_roots a b c))

OUTPUT:
Enter the numbers :
242
; Autoload: SQRT from "TRANCEND" in "C:\\GCLISP\\LISPLIB\\". Root = -1.0D0

10. To find out the number of element in a given list.


(defun no_atom(l)
(if(null l) 0
(if(atom(car l))
(+ 1(no_atom(rest l)))
(no_atom(first l)))))
(format t"~% Enter the list : ")
(setf l(read))
(format t"~% Total number of elements in list : ~a" (no_atom l))
OUTPUT:
Enter the list : (a b 2 4 c g)
Total number of elements in list : 6

You might also like