[Haskell-cafe] Curry and uncurry

Stuart Cook scook0 at gmail.com
Wed Oct 3 08:31:48 EDT 2007


On 10/3/07, PR Stanley <prstanley at ntlworld.com> wrote:
>         Without looking at the standard prelude, define the
>         higher-order library function curry that converts a function
>         on pairs into a curried
>         function, and conversely, the function uncurry
>         that converts a curried
>         function with two arguments into a function on pairs.

In other words, take a function like[1]

  add a b = a + b

and make the following possible:

  (uncurry add) (2, 3)

Conversely, take a function like

  sub' (a, b) = a - b

and make the following possible:

  (curry sub') 4 1


>         Hint: first write down the types of the two functions.

This, I think, is the key part, and it's a useful technique for
Haskell in general.

First, write down the (general) type of a curried function, and the
type of the corresponding uncurried function. Use those types to
figure out what types curry and uncurry should have. Once you've done
that, the implementation (which is pretty straightforward) should
reveal itself.


Stuart

[1] I realise that add is just (+), and sub' is just (uncurry (-)),
and some of my parens are unnecessary; I've written it this way to
make the point of the exercise clearer.


More information about the Haskell-Cafe mailing list