[Haskell-cafe] Curry and uncurry
Derek Elkins
derek.a.elkins at gmail.com
Wed Oct 3 08:56:18 EDT 2007
On Wed, 2007-10-03 at 22:31 +1000, Stuart Cook wrote:
> 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.
Indeed, modulo bottom, the implementation of these functions is
-completely- determined by the types. Djinn, for example, can
automatically generate the implementations from the types.
More information about the Haskell-Cafe
mailing list