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. http://www.haskell.org/haskellwiki/CurryingLet us look at a simple (but hopefully not too contrived, albeit it _slightly_ contrived) example:
%% currying_example.erl
-module(currying_example).
-compile([export_all]).
multiply(X, Y) -> X*Y.
doubler() -> fun(X) -> multiply(2, X) end.
In your erl shell try the following:
1> c(currying_example).
{ok,currying_example}
2> D = currying_example:doubler().
#Fun
3> D(6).
12
4> q().
ok
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):
- Scala is not a functional programming language
- Erlang is not a programming language
- The C language is purely functional
- Erlang is pragmatic
The post on C was pretty awesome.