Tuesday, July 3, 2007

Lisp Programs

Q1. sum of square of two Numbers.
sol: (defun sofsq (X Y) (+ (* X X) (* Y Y)))


Q2. Factorial of a Number
Sol: (defun factorial (N) (if (= N 1) 1 (* N (factorial (- N 1)))))


Q3. fibonacci series.
Sol: (defun fibonacci (N) (if (or (zerop N) (= N 1)) 1 (+ (fibonacci (- N 1)) (fibonacci (- N 2)))))


Q4. Palindrome
sol: (defun pal (X) (if (equal X (reverse X)) T nil ))


Q5.Exercise: The N'th triangular number is defined to be 1 + 2 + 3 + ... + N. Alternatively, we could give a recursive definition of triangular number as follows:
T(n) = 1 if n = 1
T(n) = n + T(n-1) if n > 1
Sol: (defun sum(a) (if (= a 1) 1 (+ a (sum (- a 1)))))





Q1. Reverse of a string.
sol: (defun reverse (X) (if (null X) nil (cons (first(last X)) (reverse (butlast X))) ))


Q2. Rotate left
sol: (defun rotatel (X L) (if (= X 0) L (rotatel (- X 1) (append (rest L) (list(first L)))) ))


Q3. Rotate Right
sol: (defun rotater (X L) (if (= X 0) L (rotater (- X 1) (append (last L) (butlast L))) ))


Q4. Exponent
Sol: (defun power (B E) (if (= E 0) 1 (* B (power B (- E 1)))))


Q5. count no. of elements in a list
sol: (defun count (X) (if (null X) 0 (+ 1 (count (rest X)))))





Q1. Concatenation of two lists
sol: (defun concat(X Y) (if (not (null X)) (concat (butlast X) (cons (first (last X)) Y)) y ))


Q2.To find last element of a list
sol: (defun mylast (X) (if (null (rest X)) (first X) (mylast (rest X))))


Q3. Remove first occurrence of an element from a list
sol: (defun myremove (X Y) (if (or (eql X (first Y)) (null y)) (rest Y) (cons (first Y) (myremove X (rest Y)))))


Q4. Remove all occurrence of an element from a list
sol: (defun removeall (x y) (if (equal (myremove x y) (myremove x (myremove x y))) (myremove x y) (removeall x (myremove x y)) ))


Q5. Count elements of a nested list eg. (a b (c (d (e f) g) h) (i j))
sol: (defun count (L) (if (null L) 0 (if (atom L) 1 (+ (count (first L)) (count (rest L))))))


Q6. flatten a list. eg. (Flatten '((a b c) (d e f) g)) == (A B C D E F G)
sol: (defun flatten (L) (if (null L) nil (if (atom L) (cons L ()) (append (flatten (first L)) (flatten (rest L))))))






Q1. find sum and product of a list as (sum product)
sol: (defun sp (L)(if (null L) '(0 1) (list (+ (first L) (first(sp (rest L)))) (* (first L) (first(last(sp (rest L)))) )) ))


Q2. Find intersection of two sets.
sol: (defun intersection (L1 L2) (if (null L1) NIL (if (member (first L1) L2) (cons (first L1) (intersection (rest L1) L2)) (intersection (rest L1) L2))))


Q3. Find union of two sets.
sol:(defun union (L1 L2) (if (null L1) L2 (if (member (first L1) L2) (union (rest L1) L2) (union (rest L1) (append (list(first L1)) L2 )))))


Q4. is B subset of a.
Sol: (defun subset (L1 L2) (if (null L1) T (if (member (first L1) L2) (subset (rest L1) L2) Nil )))


Q5. Find (A - B) where A & B are two sets.
sol: (defun subtract (L1 L2) (if (null L2) L1 (subtract (delete (first L2) L1) (rest L2))))


Q6. check weather two sets are equal.
sol: (defun eqset (L1 L2) (if (subset L1 L2) (if (subset L2 L1) T NIL) NIL))


Q7. write member function for nested list.
sol: (defun member (X L) (if (or (null L) (equal X (first L))) L (append (list(first L)) (member X (rest L))) ))