This document contains definitions for 10 recursive Lisp functions:
1) A function to compute the sum of squares of two numbers.
2) A function to compute the difference of squares of two numbers.
3) The Ackermann function.
4) A function to compute the factorial of a number.
5) A function to return the last element of a list.
6) A function to return a list without its last element.
7) A function to reverse a list recursively.
8) A function to remove the first occurrence of an element from a list.
9) A function to append two lists.
10) A function to return a list with alternating elements from two input
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
141 views
Lisp Programs
This document contains definitions for 10 recursive Lisp functions:
1) A function to compute the sum of squares of two numbers.
2) A function to compute the difference of squares of two numbers.
3) The Ackermann function.
4) A function to compute the factorial of a number.
5) A function to return the last element of a list.
6) A function to return a list without its last element.
7) A function to reverse a list recursively.
8) A function to remove the first occurrence of an element from a list.
9) A function to append two lists.
10) A function to return a list with alternating elements from two input
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
NCS 553 Principles of programming languages
/*Q.1> Define a LISP function to compute sum of squares */
>>(defun sumsqr (x y) (+ (* x x)(* y y))) >>(sumsqr 2 3); 13 /* Q.2> Define a LISP function to compute difference of squares .(if x > y return x2 y2 , Otherwise y2 x2 )*/ >(defun diffsqr(x y) (if(> x y) (-(* x x)(* y y)) (-(* y y)(* x x)))) >(diffsqr 2 3) 5 >(diffsqr 4 3) 7 /*Q.3> Define a Recursive LISP function to solve Ackermanns Function.
>(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)))) )))) >>(ackermann 2 3 ) 9 Department of Information Technology
NCS 553 Principles of programming languages
>>(defun digits (num) "number of digits of num" (setq a 0) (loop (setq a (+ a 1)) (setq num (floor (/ num 10))) (when (= num 0) (return a)) )) >>(digits (ackermann 2 3)) 1 /*Q.4> Define a Recursive LISP function to compute the factorial of given number. >>(defun factorial (N) "Compute the factorial of N." (if (= N 1) 1 (* N (factorial (- N 1))))) >>(factorial 5) 120 /*Q.5> Define a Recursive LISP function which takes one argument as a list and return last element of The list.(do not use last predicate.) >(defun last_element(ab_list) (first(reverse ab_list))) >(last_element '(a b c d)) D /*Q.6> a Recursive LISP function which takes one argument as a list and return list except last element of the list.(do not use butlast.) >(defun not_last(ab_list) (reverse(rest(reverse ab_list)))) >(not_last '(a b c d e)) (A B C D) Department of Information Technology
NCS 553 Principles of programming languages
/*Q.7> a Recursive LISP function which takes one argument as a list and return reverse of the list.(do not use reverse predicate).
>>(defun show-list-reverse (L)
"Create a new list containing the elements of L in reversed order." (if (null L) nil (list-append (show-list-reverse (rest L)) (list (first L))))) >>(show-list-reverse '(1 2 3 4)) (4 3 2 1) /*Q.8> a Recursive LISP function which takes two argument first an atom second a list returns a list after removing first occurrence of that atom within the list. >>(defun remove(lst elt) (cond((null lst)nil) ((equal(first lst)elt)(rest lst)) (elt(cons(first lst) (remove(rest lst)elt))))) >>(remove '(1 2 3 3 4 4)'3) (1 2 3 4 4) /*Q.9> Define a Recursive LISP function which appends two lists together. >>(defun list-append (L1 L2) "Append L1 by L2." (if (null L1) L2 (cons (first L1) (list-append (rest L1) L2)))) >>(list-append '(a b c) '(c d e)) (A B C C D E)
Department of Information Technology
NCS 553 Principles of programming languages
Q.10> Define a recursive LISP function which takes 2 lists as arguments and returns a list containing alternate elements from each list. >(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))))) )
>(alt '(1 3 5) '(6 8 9))
(1 6 3 8 5 9) >(alt '(a b c) '(d e g)) (A D B E C G) >