1. Consider the following Lisp function which applies a function f between every two adjacent elements of a list x, treating v as the final value.
(defun reduce (f x v)
(cond
((null x) v)
(t (apply f (list (car x) (reduce f (cdr x) v))))
)
)
Write appropriate calls to reduce to do the following:
(a) Add a list of numbers – test using (272 480 757 1360 1561). (b) Multiply a list of numbers – test using (4 11 25 3673).
(c) Determine the length of a list – test using (csce 4430 is easy using lisp). (d) Reversealist-testusing(esctnu).
2. Write Lisp functions to compare adjacent list elements and return a list, as described in each case. You should not use any built in Lisp functions beyond what we covered in class. The sample responses are all with the input list (a b a a a c c). Test your function on this list in each case.
(a) Remove the second and succeeding adjacent duplicates, yielding (a b a c). (b) Leave only the elements that are not repeated, yielding (a b).
(c) Leave only one copy of the repeated elements, yielding (a c).
(d) Count the number of repeated occurrences, yielding ((1 a) (1 b) (3 a) (2 c)).


0 comments