Currying
From HaskellWiki
(new exercise (uncurry const)) 
(Composing functions with multiple values) 

Line 26:  Line 26:  
But there's more that's going on than immediately meets the untrained eye. It's a twopart process. First, <haskell>div 11</haskell> is evaluated and ''returns a function'' of type <haskell>Int > Int</haskell> Then that resulting function is applied to the value <hask>2</hask>, and yields <hask>5</hask>. 
But there's more that's going on than immediately meets the untrained eye. It's a twopart process. First, <haskell>div 11</haskell> is evaluated and ''returns a function'' of type <haskell>Int > Int</haskell> Then that resulting function is applied to the value <hask>2</hask>, and yields <hask>5</hask>. 

−  +  You'll notice that the notation for types reflects this: you can read <haskell>Int > Int > Int</haskell> incorrectly as "takes two <hask>Int</hask>s and returns an <hask>Int</hask>", but what it's ''really'' saying is "takes an <hask>Int</hask> and returns something of the type <hask>Int > Int</hask>"  that is, it returns a function that takes an <hask>Int</hask> and returns an <hask>Int</hask>. (One can write the type as <hask>Int x Int > Int</hask> if you really mean the former  but since all functions in Haskell are curried, that's not legal Haskell. Alternatively, using tuples, you can write <hask>(Int, Int) > Int</hask>, but keep in mind that the tuple constructor <hask>(,)</hask> itself can be curried.) 

−  You'll notice that the notation for types reflects this: you can read <haskell>Int > Int > Int</haskell> incorrectly as "takes two <hask>Int</hask>s and returns an <hask>Int</hask>", but what it's ''really'' saying is "takes an <hask>Int</hask> and returns something of the type <hask>Int > Int</hask>that is, it returns a function that takes an <hask>Int</hask> and returns an <hask>Int</hask>. (One can write the type as <hask>Int x Int > Int</hask> if you really mean the formerbut since all functions in Haskell are curried, that's not legal Haskell. Alternatively, using tuples, you can write <hask>(Int, Int) > Int</hask>, but keep in mind that the tuple constructor <hask>(,)</hask> itself can be curried.) 

Much of the time, currying can be ignored by the new programmer. The major advantage of considering all functions as curried is theoretical: formal proofs are easier when all functions are treated uniformly (one argument in, one result out). Having said that, there ''are'' Haskell idioms and techniques for which you need to understand currying. 
Much of the time, currying can be ignored by the new programmer. The major advantage of considering all functions as curried is theoretical: formal proofs are easier when all functions are treated uniformly (one argument in, one result out). Having said that, there ''are'' Haskell idioms and techniques for which you need to understand currying. 

Line 35:  Line 35:  
* Sometimes it's valuable to think about functions abstractly without specifically giving all their arguments: this is the [[Pointfree]] style. 
* Sometimes it's valuable to think about functions abstractly without specifically giving all their arguments: this is the [[Pointfree]] style. 

* Sometimes half the work of the function can be done looking only at the first argument (but there really ''is'' only one argument, remember?): see [[functional dispatch]]. 
* Sometimes half the work of the function can be done looking only at the first argument (but there really ''is'' only one argument, remember?): see [[functional dispatch]]. 

+  * Conversion between curried and uncurried style allows [[Composing functions with multiple valuescomposition of functions with multiple values]] 

== Exercises == 
== Exercises == 
Latest revision as of 09:54, 3 January 2008
Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.
f :: a > b > c
is the curried form of
g :: (a, b) > c
f = curry g g = uncurry f
Both forms are equally expressive. It holds
f x y = g (x,y) ,
however the curried form is usually more convenient because it allows partial application.
In Haskell, all functions are considered curried: That is, all functions in Haskell take just single arguments.
This is mostly hidden in notation, and so may not be apparent to a new Haskeller. Let's take the functiondiv :: Int > Int > Int
div 11
Int > Int
Int > Int > Int
Much of the time, currying can be ignored by the new programmer. The major advantage of considering all functions as curried is theoretical: formal proofs are easier when all functions are treated uniformly (one argument in, one result out). Having said that, there are Haskell idioms and techniques for which you need to understand currying.
See
 partial application
 Section of an infix operator
 Sometimes it's valuable to think about functions abstractly without specifically giving all their arguments: this is the Pointfree style.
 Sometimes half the work of the function can be done looking only at the first argument (but there really is only one argument, remember?): see functional dispatch.
 Conversion between curried and uncurried style allows composition of functions with multiple values
[edit] Exercises
 Simplify curry id
 Simplify uncurry const
 Express usingsndorcurryand other basic Prelude functions and without lambdasuncurry
 Write the function without lambda and with only Prelude functions\(x,y) > (y,x)