Visitor No.

Sunday 13 December 2015

Recursive LISP function which takes 2 lists as arguments and returns a list containing alternate elements from each list.

10 - Define a recursive LISP function which takes 2 lists as arguments and returns a list containing alternate elements from each list.

       Implementation Code in LISP -

       (defun alt( A B)
        (cond ((and (endp A)(endp B))NIL)
        ((endp A) B)
        ((endp B) A)
        (T (cons (car A) (alt B (cdr A))))))

       OUTPUT-

        (alt’(a b c d)’(1 2 3 4))
        (A 1 B 2 C 3 D 4)

Recursive LISP function which appends two lists together.

9 - Define a Recursive LISP function which appends two lists together.

      Implementation Code in LISP - 

       (defun func(l1 l2)
       (if(null l1) l2
       (cons(first l1)(func (rest l1) l2))))

     OUTPUT-

       (func '(1 2 3) '(4 5 6))
       (1 2 3 4 5 6)

Recursive LISP function which takes two arguments first, an atom, second, a list, returns a list after removing first occurrence of that atom within the list.

8 -  Define a Recursive LISP function which takes two arguments first, an atom, second, a list, returns a list after removing first occurrence of that atom within the list.

       Implementation Code in LISP -  
    
        (defun remove(lst elt)
        (cond((null lst)nil)
        ((equal(first lst)elt)(rest lst))
        (elt(cons(first lst)
        (remove(rest lst)elt)))))

      OUTPUT-

        (remove '(1 2 3 3 4 5) 3)
         (1 2 3 4 5)

Recursive LISP function which takes one argument as a list and returns reverse of the list.

7 - Define a Recursive LISP function which takes one argument as a list and returns reverse of the list.

      Implementation Code in LISP -  
    
       ( defun fun1(l)
      ( if (null l) l 
       (append (fun1 ( cdr l)) (list (car l)))))

     OUTPUT- 

       (fun1 '(1 2 3 4) )
       (4 3 2 1)

Recursive LISP function which takes one argument as a list and returns a list except last element of the list.

6 - Define a Recursive LISP function which takes one argument as a list and returns a list except last element of the list.

     Implementation Code in LISP - 

      ( defun not_last(l)
      ( if (> (length (rest 1)) 0)
      (append (list ( first l)) (not_last (rest l)))
       nil))

     OUTPUT-

      (not_last '(1 2 3 4 5) )
      (4 3 2 1)

Recursive LISP function which takes one argument as a list and returns last element of the list.

5 - Define a Recursive LISP function which takes one argument as a list and returns last element of the list.

     Implementation Code in LISP - 

       (defun lastFirst(list) 
       (first (reverse list)))

    OUTPUT-

       (lastFirst '(1 2 3 4 5))
         5
           

Recursive LISP function to compute factorial of a given number

4 - Define a Recursive LISP function to compute factorial of a given number.

  

    Implementation Code in LISP -
      
     (defun fact (x)
     (if (= x 1) 1 
     (* x (fact (- x 1)))))

   OUTPUT-

     (fact 5)
      120

LISP function to compute difference of squares.

3 - Define a LISP function to compute difference of squares.

    Implementation Code in LISP -

      (defun difsqr(a b)
      ( if(> a b)
      (- (* a a) (* b b))
      (- (* b b) (* a a))))

    OUTPUT-

      (difsqr 3 4)
        7

LISP function to solve Ackermann’s function

2 - Define a LISP function to solve Ackermann’s function

   Implementation Code in LISP -

     (defun ackermann(m n) “The Ackermann Function”
     (cond ((= m 0)(+ n 1))
      ((= m 1)(+ n 2))
      ((= m 2)(+  3(* n 2)))
      ((= m 3)(+ 5 (* 8(- (expt 2 n) 1))))
      (t (cond ((= n 0) (Ackermann (- m 1) 1))
      (t (Ackermann (- m 1) (Ackermann m (- n 1))))
      ))))

    OUTPUT-

    (Ackermann 2 3)
    9

LISP function to compute sum of squares.

     1 - Define a LISP function to compute sum of squares. 
     
   Implementation Code in LISP -

         
      (defun sumsqr(x y)

      (
+(* x x) (* y y)))


  OUTPUT- 

    SUMSQR
   (SUMSQR 2 3) 
    13