[Haskell-cafe] Curry and uncurry

Martin Percossi haskell-cafe at martinpercossi.com
Fri Oct 5 08:07:56 EDT 2007


Warning: I hope I haven't spoiled the answer to this problem. If so, 
read only until the answer becomes clear!

I think the key is to be quite clear about what it is the function 
should do. Reading the type helps:

Prelude> :t curry
curry :: ((a, b) -> c) -> a -> b -> c

This type signature looks a bit confusing, but in fact, we can separate 
it into two chunks:

curry :: ((a, b) -> c) -> (a -> b -> c)

The first chunk is the input, and the second chunk is the output, 
obviously. So what this means is that curry takes a function which sends 
(a, b) pairs to a new type c, and returns a function which takes two 
arguments (*), a variable of type a, and one of type b, and returns a c.

So our implementation will have to take a function as its argument:

curry f = [implementation]

Also, we know that curry should itself return a function. Although we 
don't yet know the definition of that function, let's declare it and 
return it anyway:

curry f = let <definition of curriedFn> in curriedFn

But what should curriedFn do? Well we know that it should replicate the 
behaviour of f. But we also know that it's arguments need to be 
translated, so that the two arguments (one of type a, and one of type b) 
become a *single* argument of type (a, b). Can you think of a definition 
of curriedFn so that when it is called with the arguments x, y, it in 
turn produces the same result as calling f (x, y) ?

HTH
Martin

(*) yes, I am aware that the internally all functions take just one 
parameter.

PR Stanley wrote:

> No, still no idea!
> I tried curry f
> where f :: (Num a) => (a, a) -> a
> and it didn't like it.
> For some reason I'm finding this a little chalenging.
> Thanks, Paul
> At 17:03 03/10/2007, you wrote:
>
>> On 10/3/07, PR Stanley 
>> <<mailto:prstanley at ntlworld.com>prstanley at ntlworld.com> wrote:
>> I didn't even know about the curry and uncurry functions. I'm not
>> looking for the answer but some guidance would be much appreciated.
>> Thanks, Paul
>>
>>
>> You can look at the types without seeing the implementation, too. 
>> Just start up GHCI and type:
>>
>>   :t curry
>>
>> or
>>
>>   :t uncurry
>>
>> Justin
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe




More information about the Haskell-Cafe mailing list