I have this curry
function:
(define curry
(lambda (f) (lambda (a) (lambda (b) (f a b)))))
I think it's like (define curry (f a b))
.
my assignment is to write a function consElem2All
using curry
,which should work like
(((consElem2All cons) 'b) '((1) (2 3) (4)))
>((b 1) (b 2 3) (b 4))
I have wrote this function in a regular way:
(define (consElem2All0 x lst)
(map (lambda (elem) (cons x elem)) lst))
but still don't know how to transform it with curry
. Can anyone help me?
thanks in advance
bearzk
You should begin by reading about currying. If you don't understand what curry is about, it may be really hard to use it... In your case, http://www.engr.uconn.edu/~jeffm/Papers/curry.html may be a good start.
One very common and interesting use of currying is with functions like reduce or map (for themselves or their arguments).
Let's define two currying operators!
Then a few curried mathematical functions:
And then come the curried reduce/map:
Using them
First reduce use cases:
And then map use cases:
I hope that helps you grasp the usefulness of currying...
So your version of curry takes a function with two args, let's say:
and turns that into something you can call like this:
You actually have a function that takes three args. If you had a
curry3
that managed 3-ary functions, you could do something like:(like you did, but allowing cons-like functions other than cons to be used!)
and then do this:
You don't have such a
curry3
at hand. So you can either build one, or work around it by "manually" currying the extra variable yourself. Working around it looks something like:Note that there's one other possible use of curry in the map expression itself, implied by you wrapping a lambda around cons to take the element to pass to cons. How could you curry
x
intocons
so that you get a one-argument function that can be used directly to map?...