Rash thoughts about .NET, C#, F# and Dynamics NAV.


"Every solution will only lead to new problems."

Monday, 30. January 2012


Currying and uncurrying in C# and F#

Filed under: C#,F# — Steffen Forkmann at 17:00 Uhr

In the last couple of days I started to write some posts about Currying and Partial application:

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.
Tags: , , ,

3 Comments »

  1. 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

  2. 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

  3. […] 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

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> .