Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lab Manual
On
Principle of Programming
Language
(NCS-553)
Prepared By:
Rajeev Kumar
Asst. Professor
Department of IT
Table of Content:1)
2)
3)
4)
5)
Syllabus
Time Table
Academic Calendar
List of Program
Lab Program
returns a list after removing first occurrence of that atom within the list.
Program No. 1
Objective-Define a LISP function to compute sum of squares
>>(defun sumsqr(x y)
(+(* x x)(* y y)))
SUMSQR
>>(sumsqr 2 3)
13
Program No. 2
Objective-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
>(diffsqr 2 3)
5
>(diffsqr 4 3)
7
Program No. 3
Objective-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
>>(ackermann 2 3 )
9
>>(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
>>(digits (ackermann 2 3))
1
Program No. 4
Objective-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
>>(factorial 5)
120
Program No. 5
Objective-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
>(last_element '(a b c d))
D
Program No. 6
Objective- Define 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
>(not_last '(a b c d e))
(A B C D)
Program No. 7
Objective- Define a Recursive LISP function which takes one argument
as a list and return reverse of the list. (Do not use reverse predicate).
>>(defun list-append (L1 L2)
"Append L1 by L2."
(if (null L1)
L2
(cons (first L1) (list-append (rest L1) L2))))
LIST-APPEND
>>(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
>>(show-list-reverse '(1 2 3 4))
(4 3 2 1)
Program No. 8
Objective- 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.
>(defun remove(lst elt)
(cond((null lst)nil)
((equal(first lst)elt)(rest lst))
(elt(cons(first lst)
(remove(rest lst)elt)))))
REMOVE
>>(remove '(1 2 3 3 4 4)'3)
(1 2 3 4 4)
Program No. 9
Objective-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
>>(list-append '(a b c) '(c d e))
(A B C C D E)
Program No. 10
Objective-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
>(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)