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