In the last couple of days I started to write some posts about Currying and Partial application:
- Partial application in F# and C#
- Why do we need partial application? – Part 1 of n – Fluent interfaces and piping
This time I want to show you how we can write a higher-order function which allows us to curry another function. Remember the multiplication function from the first post and it’s curried form:
Currying
The question is: how can we automate this transformation process? Remember we want to have the curryied form for partial application:
Let’s look at the signature of the desired Curry-function: in our case it has to take Func<int, int, int> and returns Func<int, Func<int, int>>.
If we generalize the ints to generic parameters and fix the signature then the implementation is trivial (Compiler Driven Programming). There is exactly one way to make this work:
The F# implementation does exactly the same, but without all the annoying the type hints:
Uncurrying
Of course you can undo the currying by applying a generic Uncurry-function:
And in F# this looks like this:
Libraries
Currying and Uncurrying are two very important concepts in functional programming so they are included in a couple of libraries:
- You can find it at the top of the Prelude in FSharpx (read more).
- You can find it in the Haskell Prelude.
- You can find similar functions in Scalaz.
- Adrian Lang wrote a library called partial-js which allows to do something similar in JavaScript.
And we already have a Curry function in FSharpx for C# 😉
https://github.com/fsharp/fsharpx/blob/master/src/FSharpx.Core/CSharpCompat.fs
Though no uncurry yet, for some reason I haven’t needed it yet, but it’s worth adding it!
Comment by Mauricio Scheffer — Monday, 30. January 2012 um 17:41 Uhr
I don’t think uncurry is really useful. But for symmetry we should add it.
Comment by Steffen Forkmann — Monday, 30. January 2012 um 17:47 Uhr
[…] Currying and uncurrying in C# and F# […]
Pingback by Why do we need partial application? – Part 2 of n – Simulating type classes in C# and F# » Rash thoughts about .NET, C#, F# and Dynamics NAV. — Tuesday, 31. January 2012 um 17:13 Uhr