Trouble with currying and uncurrying...

Hi, I'm reading "The Craft of Functional Programming" and I found something I don't understand in page 185. It says: "Suppose first that we want to write a curried version of a function g, which is itself uncurried and of type (a,b) -> c. curry g This funtion expects its arguments as a pair, but its curried version, curry g, will take them separately - we therefore have to form them into a pair before applying g to them: curry :: ((a,b) -> c) -> (a -> b -> c) curry g x y = g (x,y) curry multiplyUC will be exactly the same function as multiply." OK, I have tried it and it works, but I don't understand the syntax for curry. Until now I have encountered only functions that take the same number of arguments as the function definition or less (partial application), but this syntax looks a bit new to me. curry is supposed to have as its argument one function of type (a,b) -> c and produce another function, but then the second line gives three arguments to curry, the function itself and the variables x and y. What I'm missing here? Thanks a lot, Ángel de Vicente -- http://www.iac.es/galeria/angelv/ High Performance Computing Support PostDoc Instituto de Astrofísica de Canarias --------------------------------------------------------------------------------------------- ADVERTENCIA: Sobre la privacidad y cumplimiento de la Ley de Protección de Datos, acceda a http://www.iac.es/disclaimer.php WARNING: For more information on privacy and fulfilment of the Law concerning the Protection of Data, consult http://www.iac.es/disclaimer.php?lang=en

Hi, On 25/04/11 14:20, Ozgur Akgun wrote:
On 25 April 2011 14:11, Angel de Vicente
mailto:angelv@iac.es> wrote: curry :: ((a,b) -> c) -> (a -> b -> c)
is the same as:
curry :: ((a,b) -> c) -> a -> b -> c
thanks, it makes sense now. Somehow I thought that adding the parenthesis in ...-> (a -> b -> c) was something special and that I couldn't get rid of them (in the same sense as I cannot get rid of the parenthesis in the first part and write: curry :: (a,b) -> c -> a -> b -> c Thanks, Ángel -- http://www.iac.es/galeria/angelv/ High Performance Computing Support PostDoc Instituto de Astrofísica de Canarias --------------------------------------------------------------------------------------------- ADVERTENCIA: Sobre la privacidad y cumplimiento de la Ley de Protecci�n de Datos, acceda a http://www.iac.es/disclaimer.php WARNING: For more information on privacy and fulfilment of the Law concerning the Protection of Data, consult http://www.iac.es/disclaimer.php?lang=en

On 25 April 2011 14:11, Angel de Vicente
curry :: ((a,b) -> c) -> (a -> b -> c) curry g x y = g (x,y)
Is expressing curry this way more illuminating? curry :: ((a,b) -> c) -> (a -> b -> c) curry g = \x y -> g (x,y) That is, curry is a function taking one argument that produces a result function taking two arguments. In Haskell - the type signature:
curry :: ((a,b) -> c) -> (a -> b -> c)
... can mean either form. Though this is a "quirk" of Haskell, in the language Clean, for example, the parens in the type signature "mean what they" say so only only my second definition is allowed, the first version won't compile. Best wishes Stephen

Hi, On 25/04/11 14:21, Stephen Tetley wrote:
On 25 April 2011 14:11, Angel de Vicente
wrote: curry :: ((a,b) -> c) -> (a -> b -> c) curry g x y = g (x,y)
Is expressing curry this way more illuminating?
curry :: ((a,b) -> c) -> (a -> b -> c) curry g = \x y -> g (x,y)
That is, curry is a function taking one argument that produces a result function taking two arguments.
In Haskell - the type signature:
curry :: ((a,b) -> c) -> (a -> b -> c)
... can mean either form.
Though this is a "quirk" of Haskell, in the language Clean, for example, the parens in the type signature "mean what they" say so only only my second definition is allowed, the first version won't compile.
This was my first encounter with this syntax, but it was a bit confusing. curry :: ((a,b) -> c) -> a -> b -> c is much clearer to me, and once partial application is understood it is no problem to see what curry f or curry f x y mean. But to me (at this point in my Haskell trip) it would make more sense that once you put the parentheses as in the original definition, no partial application is allowed and so the lambda notation is required. Thanks a lot, Ángel de Vicente -- http://www.iac.es/galeria/angelv/ High Performance Computing Support PostDoc Instituto de Astrofísica de Canarias --------------------------------------------------------------------------------------------- ADVERTENCIA: Sobre la privacidad y cumplimiento de la Ley de Protección de Datos, acceda a http://www.iac.es/disclaimer.php WARNING: For more information on privacy and fulfilment of the Law concerning the Protection of Data, consult http://www.iac.es/disclaimer.php?lang=en

On 25 April 2011 14:11, Angel de Vicente
OK, I have tried it and it works, but I don't understand the syntax for curry. Until now I have encountered only functions that take the same number of arguments as the function definition or less (partial application), but this syntax looks a bit new to me. curry is supposed to have as its argument one function of type (a,b) -> c and produce another function, but then the second line gives three arguments to curry, the function itself and the variables x and y.
What I'm missing here?
You can think of all functions in Haskell as taking only one argument. So curry has one argument, which is a function that takes a pair (in this case g). The value of curry g is another function (the curried version of g) which takes two arguments. This is clearer when written out like so: g :: (a, b) -> c h :: a -> b -> c h = curry g h x y = g (x, y) So by simple substitution we can see that curry g x y = g (x, y) It might be even clearer if we add parentheses, since function application associates to the left: (curry g) x y = g (x, y) Hope this clears things up. For more information you could have a look at the Gentle Introduction's section on functions: http://www.haskell.org/tutorial/functions.html Benedict.
participants (4)
-
Angel de Vicente
-
Benedict Eastaugh
-
Ozgur Akgun
-
Stephen Tetley