[erlang-questions] Automatic currying
Richard A. O'Keefe
ok@REDACTED
Fri May 1 02:46:48 CEST 2015
On 1/05/2015, at 8:42 am, Siraaj Khandkar <siraaj@REDACTED> wrote:
>
> curry(F) ->
> Info = erlang:fun_info(F),
> {value, {arity, Arity}} = lists:keysearch(arity, 1, Info),
> curry(F, [], Arity).
You do know that you can write
{arity, Arity} = erlang:fun_info(F, arity) ?
>
> curry(F, Args, 0) ->
> apply(F, lists:reverse(Args));
> curry(F, Args, Arity) ->
> fun (X) -> curry(F, [X | Args], Arity - 1) end.
This is where it gets hairy. The thing is that in a language
like Haskell or Clean, there is no difference between
f
and
f {- applied to no arguments -}
In Erlang, there is a big difference between
F
and
F()
What if I have
F = fun (X) -> ... end
and I want
(curry(F))(X)
to give me fun () -> ... X ... end?
Having
curry(F, Args, 0) ->
Rev = lists:reverse(ARgs),
fun () -> apply(F, Rev).
would allow people to invoke the result or not, as they chose.
I suggest *not* doing any of this.
Haskell tends to involve stuff like (`f`y) for
providing the second argument, which looks distinctly
odd when f has more than 2, and don't let's mention
`flip' and its relatives.
Why do I suggest not doing this?
First, as already mentioned, the fact that Erlang
functions are strict and effectful means that the
distinction between F and F() is vital.
Second, this isn't really how Erlang is _meant_ to
be used, so unlike Haskell, the compiler is not
going to put much effort into making this less slow.
Most important, other people aren't going to expect
to read Erlang code written this way, so it's not
going to be as maintainable as possibly longer code
that's more direct.
More information about the erlang-questions
mailing list