Difference between revisions of "Currying"

From HaskellWiki
Jump to: navigation, search
(Composing functions with multiple values)
(c/e; note associativity of arrows in types and of application)
Line 21: Line 21:
 
however the curried form is usually more convenient because it allows [[partial application]].
 
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.''
+
In Haskell, ''all'' functions are considered curried: That is, ''all functions in Haskell take just one argument.''
This is mostly hidden in notation, and so may not be apparent to a new Haskeller. Let's take the function <haskell>div :: Int -> Int -> Int</haskell> which performs integer division. The expression <hask>div 11 2</hask> unsurprisingly evaluates to <hask>5</hask>.
+
This is mostly hidden in notation, and so may not be apparent to a new Haskeller. It can be said that arrows in the types notation associate ''to the right'', so that <hask>f :: a -> b -> c</hask> is really <hask>f :: a -> (b -> c)</hask>. Functional application though, associates ''to the left'': <hask>f x y</hask> is really <hask>(f x) y</hask>. <br><br>
  +
  +
Let's take the function <haskell>div :: Int -> Int -> Int</haskell> which performs integer division. The expression <hask>div 11 2</hask> unsurprisingly evaluates to <hask>5</hask>.
   
 
But there's more that's going on than immediately meets the untrained eye. It's a two-part 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 two-part 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>.

Revision as of 11:07, 12 January 2016

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

You can convert these two types in either directions with the Prelude functions curry and uncurry.

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 one argument. This is mostly hidden in notation, and so may not be apparent to a new Haskeller. It can be said that arrows in the types notation associate to the right, so that f :: a -> b -> c is really f :: a -> (b -> c). Functional application though, associates to the left: f x y is really (f x) y.

Let's take the function
div :: Int -> Int -> Int
which performs integer division. The expression div 11 2 unsurprisingly evaluates to 5. But there's more that's going on than immediately meets the untrained eye. It's a two-part process. First,
div 11
is evaluated and returns a function of type
Int -> Int
Then that resulting function is applied to the value 2, and yields 5. You'll notice that the notation for types reflects this: you can read
Int -> Int -> Int
incorrectly as "takes two Ints and returns an Int", but what it's really saying is "takes an Int and returns something of the type Int -> Int" -- that is, it returns a function that takes an Int and returns an Int. (One can write the type as Int x Int -> Int 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 (Int, Int) -> Int, but keep in mind that the tuple constructor (,) 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.

See

Exercises

  • Simplify curry id
  • Simplify uncurry const
  • Express snd using curry or uncurry and other basic Prelude functions and without lambdas
  • Write the function \(x,y) -> (y,x) without lambda and with only Prelude functions