Anda di halaman 1dari 5

Functional Programming

Questions and Exercises

1. What is an S-expression? What is a form? Give an example of an Sexpression that is not a form. 2. Illustrate the tree description and the simplied description of the result of evaluating the following expression: >((a (b c)) b (b.(c.(d.(e.(f)))))) 3. What is the dierence between predicates equal and eql? Give an example when these predicates compute dierent results. 4. What is the nal result of evaluating the following Lisp instructions: >(defun f (x) (+ x x)) >(setf g 4) >(defun g (x) (funcall #f (f x))) >(g g) 5. The built-in predicate consp recognizes dotted pairs. Use this predicate to dene the predicate islist which recognizes lists. 6. Dene, using append, the functions make1 and make2 such that (make1 <e1> <e2>) yields the dotted pair (<e1> . <e2>) (make2 <e1> <e2>) yields the list (<e1> <e2>) 7. What is recursion? What is a recursive function? 8. What is a tail recursive function? 9. Give an example of a recursively dened set. 10. Consider the function with the following recursive denition if n = 0 1 2 if n = 1 f : N N, f (n) = 2 f (n 1) f (n 2) if n > 1

(a) Write a recursive denition of function f in Lisp. (b) Is the function f simple recursive? Is it double recursive? Motivate your answer. (c) Illustrate the reversed tree for the computation of (f 3). (d) What is the recursion depth of (f 3)? How many recursive calls of f are performed? (e) Find a tail recursive denition of f. 11. (a) What does the function (mistery l n) dened below compute? >(defun mistery (l n &optional (m (length l))) (if (>= n m) l (mistery (cdr l) n (- m 1)))) 12. Describe the evident purpose of the following procedure >(defun strange (l) (cond ((null l) nil) ((atom l) l) (t (cons (strange (first l)) (strange (rest l)))))) 13. (a) Dene a recursive function (first l n) which returns the list of rst n elements of a list l. If l has less than n elements, the result should be list l. (b) Write a tail-recursive version of the previous function. (c) Dene a function (last l n) which returns the last n elements of a list l. If l has less than n elements, the result should be l. (d) Assume l is a list with p elements, and 0 m n p. Use the functions first and last mentioned before to dene the function (sublist m n l) which yields the elements of list l from position m to position n. 14. Consider the representation of complex numbers a + b i as dotted pairs (a . b) where a and b are real numbers. (a) Dene a predicate (valid-complex <e>) which returns true if <e> is such a dotted pair, and false otherwise. You can make use of the built-in predicates realp and consp. (b) Dene the operations (complex-sum <e1> <e2>) for the addition and (complex-prod <e1> <e2>) for the multiplication of complex numbers represented in this way. Keep in mind that: (a + b i) + (c + d i) = (a + c) + (b + d) i (a + b i) (c + d i) = (a c b d) + (a d + b c) i 15. Consider the representation of rational numbers a/b as dotted pairs (a . b) where a and b are numbers. 2

(a) Dene a predicate (valid-Q <e>) which returns true if <e> is such a dotted pair, and false otherwise. You can make use of the built-in predicates numberp and consp. (b) Dene the predicate (q-equal <e1> <e2>) if <e1> and <e2> are dotted pair representations of the same rational number. Keep in a c mind that = if and only if a c = b d. b d (c) Dene the operations (rational+ <e1> <e2>), (rational- <e1> <e2>), (rational* <e1> <e2>), and (rational/ <e1> <e2>) for the addition, subtraction, multiplication, and division of rational numbers represented in this way. 16. (a) What does the following function do? >(defun l-to-mv (l) (apply #values l)) (b) Dene the multivalued function (divisors n) which returns all the divisors of a number n except itself. (c) A positive integer n is a magic number if it is equal to the product of its divisors. For example, 6 is a magic number because the divisors of 6 less than 6 are 1,2,3, and 6 = 1 2 3. Dene a predicate (magicp n) which is true if and only if n is a magic number. You can make use of divisors, multiple-value-list, and apply. 17. Dene a multiple-value function (sqr-eq b c) to nd the real roots of the equation x2 + b x + c = 0. The function should be sensitive to the sign of the discriminant = b2 4 c. If < 0 then zero multiple values must be returned. b If = 0 then return the value of . 2 b b + If > 0 then return two values: and . 2 2 18. Figure out what are the values assigned to the arguments of the lambda expression and what is the result computed by the following call: >((lambda (x y &optional (o1 x s1) (o2 (if s1 o1 y)) &key (k (list x y o1 o2) s2) &aux (a (or s1 s2))) (list x y o1 o2 s1 s2)) a b c)

19. Dene the function (inner f l1 l2 g) which, for functions f and g and lists of same length l1 = (a1 . . . an ), l2 = (b1 . . . bn ) computes the result (g (f a1 b1 ) ...(f an bn )). For example: >(inner #+ (1 2) (3 4) #*) should yield (1 + 3) (2 + 4), that is, 24. 20. Explain if the following macro denition of factorial is correctly dened or not >(defmacro fact (n) (if (zerop ,n) 1 (* n (fact (- n 1))))) (a) What happens if we call (fact 0)? Write the expanded form in this case. (b) What happens if we call (fact 2)? Write the expanded form in this case. (c) Correct the macro denition of fact, if necessary. 21. (a) Dene a function which eliminates the duplicates from a list of atoms. (b) Dene a function which eliminates the duplicates from all sublists of a list. 22. (a) Dene a recursive function (my-replace <list> <e> <f>) which replaces the rst occurrence of element <e> in list <list> with element <f>. (b) Dene the same function in a tail recursive way. (c) Dene the recursive function (my-replace-all <list> <e> <f>) which replaces all occurrences of element <e> in list <list> with element <f>. 23. (a) Indicate the vulnerabilities of macro denitions. (b) The following macro denition is intended to dene a macro that evaluates the forms in the body for all integer values p starting from m+1 to n. >(defmacro my-do ((p m n) &rest body) (do ((,p (+ ,m 1) (+ ,p 1)) (n-value ,n)) ((> ,p n-value) (values)) ,@body)) Show that this macro denition has the variable capture problem. Correct the macro denition to avoid variable capture. 4

24. What are the values assigned to the arguments of the lambda expression and what is the result computed by the following call: >((lambda (x y &optional (o1 x s1) (o2 (if s1 o1 y)) &key (k (list x y o1 o2) s2) &aux (a (or s1 s2))) (list x y o1 o2 s1 s2)) a b c)

Anda mungkin juga menyukai