views:

620

answers:

4

I'm using the SICP lectures and text to learn about Scheme on my own. I am looking at an exercise that says "An application of an expression E is an expression of the form (E E1,...En). This includes the case n=0, corresponding to an expression (E). A Curried application of E is either an application of E or an application of a Curried application of E."

(Edit: I corrected the above quote ... I'd originally misquoted the definition.)

The task is to define a Curried application of the procedure which evaluates to 3 for

(define foo1
    (lambda (x)
        (* x x)))

I really don't understand the idea here, and reading the Wikipedia entry on Curriying didn't really help.

Can anyone help with a more lucid explanation of what's being asked for here?

Actually even giving me the answer to this problem would be helpful, since there are five more to solve after this one. ... I just am not getting the basic idea.

Addition: Even after Brian Campbell's lengthy explanation, I'm still somewhat lost.

Is:

(foo1 (sqrt 3)))

Considered an application of foo, and therefore a curried application of foo?

Seems too simple, but maybe...

Typing

(((foo1 2 )) 2)

Into DrScheme gives an error (which I kind of expected)

procedure application: expected procedure, given: 4 (no arguments)

After re-reading What is Currying? I understand I can also re-define foo1 to be:

(define (foo1 a)
    (lambda (b)
        (* a b)))

So then I can type

((foo1 3 ) 4)

12

But this doesn't really get me any closer to producing 3 as an output, and it seems like this isn't really currying the original foo1, it's just re-defining it.

Damn, 20 years of C programming hasn't prepared me for this. :-) :-)

+6  A: 
Brian Campbell
I appreciate your lengthy answer, but confess I'm still fairly puzzled. I edited the question to reflect my current state of confusion :-)
Leonard
@Brian. See my comment in the original question. Thx.
Leonard
+1  A: 

See What is 'Currying'?

Currying takes a function and provides a new function accepting a single argument, and returning the specified function with its first argument set to that argument.

eed3si9n
Thanks. I had read that question before posting, but I re-read it now, and it made more sense the second time.
Leonard
A: 

I think you are confusing yourself too much. Currying a function takes a function of type F(a1,a2,...aN) and turns it into F(a1) which returns a function that takes a2, (which returns a function that takes a3 ... etc.)

So if you have:

(define F ((lambda a b) (+ a b)))
(F 1 2) ;; ==> 3

you can curry it to make something that acts like the following:

(define F ((lambda a) ((lambda b) (+ a b))))
((F 1) 2) ;; ==> 3

in the case of your specific question, it seems very confusing.

(foo1 (sqrt 3))

seems to be suitable. I would recommend leaving it for now and reading more of the book.


you can actually make a function that does a simple curry for you:

(define (curry f x) ((lambda y) (apply f (cons x y)))
(curry = 0) ;; a function that returns true if input is zero
James Brooks
You seem to have misplaced some parentheses around lambda.
Svante
+2  A: 

I don't think James' curry function is correct - there's a syntax error when I try it in my scheme interpreter.

Here's an implementation of "curry" that I use all the time:

> (define curry (lambda (f . c) (lambda x (apply f (append c x))))))
> ((curry list 5 4) 3 2)
(5 4 3 2)

Notice, it also works for currying more than one argument to a function.

There's also a macro someone has written that let's you write functions that implicitly curry for you when you call them with insufficient arguments: http://www.engr.uconn.edu/~jeffm/Papers/curry.html

Paul Hollingsworth
+1. I prefer your function, even if I typed mine in properly yours works better. And I should have checked my functions worked, sorry.
James Brooks