[erlang-questions] Self-referencing anonymous functions?

Christian Axelsson <>
Fri Apr 3 10:45:33 CEST 2009


Joe Armstrong wrote:
> 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 :-)

I've noticed that aswell :)
Would it be a bad idea to introduce a keyword for self referencing
anonymous functions?

/Christian

> /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
>>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions> Now you know why the book didn't have any examples of this :-)
>
>
> /Joe
>




More information about the erlang-questions mailing list