First, here's a quick review on currying: a curried function to handle multiplication can be defined as:
(define mult
(lambda (a)
(lambda (b)
(* a b))))
(a) Define a function curry3 that takes a
three-argument function and returns a curried version of that
function. Thus ((((curry3 f) x) y) z) returns the result
of (f x y z).
Sometimes when you define a curried function, the internal function
needs to be recursive. This raises the question: how do you make a
recursive function call in a function defined only by
lambda? The following code snippet is a sample. This is a
curried function that multiples the first parameter by the factorial
of the second.
(define multfact
(lambda (a)
(letrec ((factorial
(lambda (b)
(if (= b 1) a
(* b (factorial (- b 1)))))))
factorial)))
letrec defines the local variable
factorial to be bound to a function. That function is
then returned as the result of the letrec function
call.
(b) Using the above code for inspiration if appropriate, define a
function called curried-map that works like Scheme's
built-in map function except for the fact that it is a
curried version of map. For example, if you had a
function called add-one that takes one parameter and adds
1 to it, a call to curried-map might look like:
((curried-map add-one) '(1 2 3)) --> (2 3 4)
(c) Define a curried function called
curried-map-first-two that works like
curried-map except that the function argument is always a
function of two arguments (like the operator +). Use the first and
second elements of the list argument as the first pair of arguments to
the function argument, then the second and third elements, then the
third and the fourth, and so on. If there are fewer than two elements
in the list, the empty list should be returned. Here is an
example:
((curried-map-first-two +) '(2 3 4 5 7)) ---> (5 7 9 12)
You must use recursion, and not iteration. You may not use side-effects (e.g. set!).
Submit your code in a file called curry.scm.