[erlang-questions] Self-referencing anonymous functions?

Joe Armstrong <>
Thu Apr 2 20:19:17 CEST 2009


1> Cost = fun(F, [{N,C}|T]) -> N*C + F(F, T);(_,[])-> 0 end.
#Fun<erl_eval.12.113037538>
2> Cost(Cost, [{2,3},{4,5}]).

Now you know why the book didn't have any examples of this :-)


/Joe


On Thu, Apr 2, 2009 at 8:04 PM, Thomas Allen <> wrote:
> Hi all,
>
> I'm just getting going with Erlang (using Armstrong's book) and I ran
> into anonymous functions. I tried translating the shop:cost and
> shop:total functions, but I cannot translate the latter because it
> refers to itself. How would I rewrite this to not throw errors?
>
> Total = fun ([{What, N}|T]) ->
>        Cost(What) * N + Total(T); % Clearly, "Total" won't fly here
>    ([]) -> 0
> end.
>
> Thanks,
> Thomas
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list