Article on FP praising Erlang!
Yariv Sadan
yarivvv@REDACTED
Wed Jun 21 03:42:48 CEST 2006
On 6/20/06, Richard A. O'Keefe <ok@REDACTED> wrote:
> "Yariv Sadan" <yarivvv@REDACTED> wrote:
> - Erlang doesn't support currying, does it? Will it ever?
>
> Let's be clear about what we're asking for.
> "Currying" is the process of taking a function
> f : A x B -> C
> and returning a function
> f' : A -> B -> C
>
> We can do that right now:
>
> curry2(F) -> fun (A) -> fun (B) -> F(A,B) end end.
> curry3(F) -> fun (A) -> fun (B,C) -> F(A,B,C) end end.
> curry4(F) -> fun (A) -> fun (B,C,D) -> F(A,B,C,D) end end.
>
> plus(X, Y) -> X + Y.
>
> Now we can write things like
>
> lists:map(curry2(plus)(42), [1,2,3])
>
> Except we can't. curry(plus)(42) is a syntax error.
> I suppose to count as "supporting" currying, Erlang would have to allow this.
> So we change that to
>
> F = curry2(plus),
> lists:map(F(42), [1,2,3])
>
> and of course _that_ doesn't work either, because 'plus' is an atom,
> not a function. So we see that the way to curry a function in Erlang
> is
>
> F = curry2(fun plus/2),
> lists:map(F(42), [1,2,3]).
>
> which is admittedly clumsy, but it does work, so arguably Erlang supports
> currying right now, and has ever since funs were introduced.
>
> I would tend to add
>
> curry2(F, A) -> fun (B) -> F(A,B) end.
> curry3(F, A) -> fun (B,C) -> F(A,B,C) end.
> curry4(F, A) -> fun (B,C,D) -> F(A,B,C,D) end.
>
> and then use
>
> lists:map(curry2(fun plus/2, 42), [1,2,3])
>
> Having used Clean and Haskell, where functions are "born" curried, and
> SML, where traditional usage (hard-wired into binary operators) passes
> tuples to functions, and Lisp and R, where functions are not generally
> unary at all,
> I have to say that curried functions work well in a language built from
> the ground up for that kind of use, but tend to be clumsy in thought and
> in use in other languages. I _could_ write currying functions for R,
> but I can't think when I'd ever want to use them, for example. The
> "natural" thing to do in languages like Lisp, R, and Erlang, is to
> define ad-hoc lambdas (funs) on the few occasions when currying would have
> been any use.
>
Richard, thanks for the explanation. I'm still learning Erlang and I
don't have a full grip on its nuances yet :) So, one could use fun
expressions in Erlang to acheive the same end as currying, or
"binding" a parameter to a function. I was wondering whether Erlangers
think it would be desirable to add "built in" support for currying to
Erlang, similar to other languages such as OCaml and Haskell. With
"build in" currying, the plus() example could be written as
(hypothetically):
lists:map(plus(42), [1,2,3]).
I'm sure this issue has been discussed extensively, but I thought this
is a good opportunity to ask given that the article mentioned Currying
as a parallel to the Adapter pattern in the OOP world.
Regards,
Yariv
More information about the erlang-questions
mailing list