Erlang doesn't support currying? News to me!

Written on 4:13:00 PM by S. Potter

I keep seeing developers who only glance at Erlang and think that currying isn't supported. Nothing could be further from the truth...unless you are defining currying support only in terms of specific syntactic sugar in a language for currying. So let us step back and define what currying support really means:

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.
Let us look at a simple (but hopefully not too contrived, albeit it _slightly_ contrived) example:

%% currying_example.erl


multiply(X, Y) -> X*Y.
doubler() -> fun(X) -> multiply(2, X) end.
In your erl shell try the following:

1> c(currying_example).
2> D = currying_example:doubler().
3> D(6).
4> q().
As you can see in the module code above, Erlang supports what is called anonymous functions (e.g. fun (X) -> somePredefineFunction(OtherVarOrConst, X) end), which then allows us to manufacture single argument functions in a way that is clear and expressive in the code IMO. For those that haven't been following the functional programming debates over the last week or two around the blogosphere, please see the following blog posts (some are pretty funny and will make you smile at least twice with their intended absurdity): Previous posts on this blog that are related are: I will try to put together a few snippets on currying support in Python, Javascript and Ruby tonight after my Greek class (which I am late for now) if I have time as a quick exercise:)

If you enjoyed this post Subscribe to our feed


  1. Anonymous |

    The post on C was pretty awesome.

  2. Derek |

    A better example might be:

    %% currying_example.erl


    multiply(X, Y) -> X*Y.
    multiplier(W) -> fun(X) -> multiply(W, X) end.

    That way you can have:

    2> D = currying_example:multiplier(3).
    3> D(6).


Post a Comment