RaghavGupta - AI - Lab Experiment 2
RaghavGupta - AI - Lab Experiment 2
LAB FILE
FOR
1 15/09/21 Practical 01: Design a DFA which will accept all the strings containing 02
odd number of 1's over an alphabet {0, 1} and write a program to
implement the DFA.
10
Practical No 1: Write following programs in LISP.
Date: 15 September 2021
Code:
(defun sumsqr(x y)
(+(* x x)(* y y)))
(write(sumsqr 2 3))
Output:
2
ii. Write a LISP function to compute the difference of
squares. (if x>y return x 2 – y2, otherwise y2 - x2)
Code:
(defun diffsqr(x y)
(if(> x y)
(-(* x x)(* y y))
(-(* y y)(* x x))))
(write(diffsqr 2 3))
Output:
3
iii. Write a Recursive LISP function which takes one
argument as a list and returns the last element of the list.
Code:
(defun last_element(ab_list)
(first(reverse ab_list)))
4
iv. Write a Recursive LISP function which takes one
argument as a list and return list except the last element of
the list.
Code:
(defun not_last(ab_list)
(reverse(rest(reverse ab_list))))
Output:
5
v. Write a Recursive LISP function which takes one
argument as a list and returns the reverse of the list.
Code:
(if (null L)
nil
(list-append (show-list-reverse (rest L))
(list (first L)))))
Output:
6
vi. Write a Recursive LISP function which takes two
arguments, first an atom and second a list, returns a list
after removing the first occurrence of that atom within the
list.
Code:
Output:
7
vii. Write a Recursive LISP function which appends two
lists together.
Code:
Output:
8
viii. Write a Recursive LISP function which takes two lists
as arguments and returns a list containing alternate
elements from each list
Code:
(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: