You are on page 1of 3

TES3111 Artificial Intelligence

Assignment-I (10%) Release date: Week 5 ( 9-7-2011) Submission date: During Lab time (Week 7)
Assignment-1 Note : Marks will be scaled to 10.
1. This is individual assignment and you will have to demonstrate at least one problem

(50 Marks)

during demonstration on Week 7 during your respective Lab sessions.


2. Write lisp code for the following questions keeping the following points in your mind.

The % shown against each gives the marking weight for each question.
3. It must be your code with your logic so that you can explain and your code must have

comments along with a trace of compilation and correct execution for a sample input. (60%) 4. The source code in either plain text or courier font and supporting files need to be submitted at the time of demonstration. The demonstration may be done on your input data or input data given by your tutor. (40%) To be completed and submitted at the end of your lab session during 7th week .
1. Write a function vecmul that will take as inputs two simple lists of numbers. vecmul

should multiply these lists coordinate-wise as one would multiply vectors. Assume the two lists are the same length. [For example, (vecmul '(2 3 4 5) '(1 4 5 2)) returns (2*1 3*4 4*5 5*2) or (2 12 20 10).] (defun vecmul (x y) (mapcar # * x y)) ;This function will multiply all number in the list.
2. Use addvec and vecmul to define an inner product function innprod. [For example,
(innprod '(1 2 3) '(7 8 9))

returns 1*7 + 2*8 + 3*9 or 50.]

(defun vecmul (x y) (mapcar # * x y))

;This function will multiply each element in the list. (defun addvec (lista listb) (apply # +(vecmul lista listb))) ;This function will add all the element in the list.

(defun innprod (list1 list2) (addvec list1 list2))

;This function is to combine all the function of addvec list1 and list2.

3. Define a function last1 which returns the last element of a list (at the top level) without

making use of reverse or last (already built in to Common Lisp.) (defun last1 (list) ;This function declare to accept list. (if (cdr list) ;This function is to determine the list that will be executed. (last1 (cdr list)) ;This function is to take back of the first element. (car list))) ;This function is to get the last part on the list.
4. Write a ``super'' reverse function srev which reverses all lists that occur in an S-

expression at any level. Thus (srev '((a b (c d)) e (f g))) should yield ((g f) e ((d c) b a)). [Hint: This is similar to the previous reverse1, except for additional recursion.] (defun srev (list) ;This function define the srev function. (if (atom list) ;This function determine the list that will be executed. list (reverse (mapcar #'srev list)) ;This function is to check and reverse the list. ))
5. Write a function insert1 which takes its first integer argument and inserts it at the

correct place in its second argument, a list of integers sorted in increasing order, so that (insert1 3 '(1 4 6 6 7)) produces (1 3 4 6 6 7). [insert1 only works for sorted lists.] (defun insert1 (x y) ;This function is to define the insert1 (sort (cons x y) #'<)) ;This function is to check if the number that will be added is bigger or smaller than it previous number. ;This function also used so that the previous number will follow the number added.
6. Use insert to write a function sort1 which sorts a list of integers into increasing order.

[We are done if the list is nil. Otherwise insert the car of the list into the sorted cdr.]

(defun sort1 (list) (if (null list) () (insert1 (first list) (mysort (cdr list))) ))

;This function is to define the sort1 function. ;This function is to check for null list. ;This function is to insert number into the list. ;This function will sort the list.

7. Write a function which takes a mixed list (numbers and other items) and returns the list

with all numbers incremented, other items should be left as is, eg: (inc-list1 '(cat 4 dog 3 x 5)) => (cat 5 dog 4 x 6)

8. Write a function that takes a list as its only argument and returns that list after enclosing

every item in it in an extra set of brackets. eg: (brack-lis '(b l o b)) => ((b) (l) (o) (b)) (defun brack-lis(alist) (loop for x in alist collect (let (newlist '()) (push x newlist)))) ;This function is to define brackets function. ;This function is to iterate through a list. ;This function is to collect newlist. ;This function pushes x into the new list.

9. Develop a function which takes a list of integers as its only argument and returns a list of

the factorials of those numbers. (fac-lis '(2 4 3)) => (2 24 6) (defun factorial (n) (if (= n 1) 1 (* n (factorial (- n 1))))) (defun fac-lis(y) (mapcar #'(lambda (x)(factorial x)) y)) ;This function is to define the factorial. ;check if n is equal to 1 ;factorial formulae. ;This function define fac-list. ;Operate the factorial calculation using the define factorial function.

10. Write a function del-item which deletes all occurrences of an item within a list, eg:

(del-item 'spam '(egg spam chips spam beans and spam)) (egg chips beans and) (defun del-item (aItem aList) (remove aItem aList)) ;This function is to define del-item. ;This function is to remove an item from the list.

You might also like