[erlang-questions] Functions with same name and varying arity using apply

Dirk Scharff dirk.scharff@REDACTED
Thu Jan 20 00:31:33 CET 2011


Hi Taylor,

I don't get what you want to do. 
From your example I assume that you want to apply a function (in your case addition) to each element of a list?
You could to the same calculation as follows:
lists:map(fun(X)->lists:sum(X) end,[[1],[2,3]]).

However I'm not sure if I got your question and if this is helpful at all. 

Regards,
Dirk.

Am 20.01.2011 um 00:03 schrieb Taylor Venable:

> Hi there, is the following possible in Erlang? If I use the following code:
> 
> %% test.erl
> 
> -module(test).
> -export([foo/1,foo/2,bar/2]).
> 
> foo(A) -> A.
> foo(A, B) -> A + B.
> 
> bar(_, []) -> [];
> bar(F, [X|Xs]) -> [apply(test, F, X) | bar(F, Xs)].
> 
> %% END
> 
> 2> test:bar(foo, [[1], [2,3]]).
> [1,5]
> 
> Is there a way to do the same thing without exporting foo/1 and foo/2?
> It seems not with apply/3, but maybe some technique with apply/2, or
> another way? Thanks for any ideas.
> 
> -- 
> Taylor C. Venable
> http://metasyntax.net/
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> 



More information about the erlang-questions mailing list