Curry and uncurry

Hi The following is from the Hutton book: 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. Hint: first write down the types of the two functions. 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

On 10/3/07, PR Stanley
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.

On Wed, 2007-10-03 at 22:31 +1000, Stuart Cook wrote:
On 10/3/07, PR Stanley
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.

On 10/3/07, PR Stanley
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
participants (4)
-
Derek Elkins
-
Justin Bailey
-
PR Stanley
-
Stuart Cook