1. To
find out the average of few integers:
----------------------------------------------------------------------
Implementation Code in LISP -
(defun func(l1 l2)
----------------------------------------------------------------------
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)
(defun
averagenum (n1 n2 n3 n4)
(/
( + n1 n2 n3 n4) 4)
)
(write(averagenum
10 20 30 40))
---------------------------------------
2. Reading Input from Keyboard:
write
( + 15.0 (read)))
---------------------------------------
3.
factorial program using recursive:
(defun
factorial (n)
(if
(= n 0)
1
(* n (factorial (- n 1))))
)
>>(factorial
3)
----------------------------------------------------------------------
4.
Using iteration run the above pro.
(loop
for
i
from
0
to
16
do
(format
t
"~D!
= ~D~%"
i
(factorial
i))
)
----------------------------------------------------------------------
5.
Sorting Numerical List
The < function
is used when sorting a numeric list. For example,
(sort
'(4 8 21 17 33 7 21 7) '<)
produces
this:
(4
7 7 8 17 21 21 33)
----------------------------------------------------------------------
6.
Append two string lists :
(append '("a" "b" "c")
'("d" "e" "f"))
----------------------------------------------------------------------
----------------------------------------------------------------------
7.
List Manipulating Functions
The
following table provides some commonly used list manipulating
functions.
- (setq l1 ‘(3 4 5))
(setq
l2 ‘(4 7 8))
(append
l1 l2)
Output:-
(3 4 5 4 7 8)
- car (car (list 4 6)) (car (list "f" "y" "i")) : It takes a list as argument, and returns its first element.
- cdr (cdr (list 4 6)) (cdr (list "f" "y" "i")) : It takes a list as argument, and returns a list without the first element
- cons (cons 1 2) :It takes two arguments, an element and a list and returns a list with the element inserted at the first place.
- Command list: It takes any number of arguments and returns a list with the arguments as member elements of the list.
- Command append: It merges two or more list into one.
- Command last: It takes a list and returns a list containing the last element.
- Command member: It takes two arguments of which the second must be a list, if the first argument is a member of the second argument, and then it returns the remainder of the list beginning with the first argument.
- Command reverse: It takes a list and returns a list with the top elements in reverse order.
- Command list (list 4 6) or '(4 6) : create a list
----------------------------------------------------------------------
8.
assignment : 8 Program
for merging two unsorted-students-name-list in sorted order
;sorting
the strings
(sort
(list "Amal" "Akbar" "Anthony")
#'string<)
;merging
the strings
(merge
'list (list "Rishi" "Zara" "Priyanka")
(list "Anju" "Anuj" "Avni") #'string<)
----------------------------------------------------------------------
9
– Assignment: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)
(cons(first l1)(func (rest l1) l2))))
OUTPUT-
(func '(1 2 3) '(4 5 6))
(1 2 3 4 5 6)
----------------------------------------------------------------------
10
– assignment : 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)
Endp
predicate checks to see if
- is an empty list. T is
returned if list is empty.
>>
(endp ‘())
>>T
(because list is empty)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.