سؤال

Erlang 17 was released. And according to Erlang OTP 17.0 has been released:

Funs can now be given names

No examples are given.

Any ideas how to assign names to funs in Erlang 17?

هل كانت مفيدة؟

المحلول

Joe Armstrong explains it in his blog post with an example.

1> F = fun F(0) -> 1; 
           F(N) -> N * F(N - 1) 
       end.
#Fun

Previously you have to pass in the function as one of the args for anonymous recursive calls. (Think of y-combinator).

1> F = fun(F, 0) -> 1;
          (F, N) -> N*F(F, N-1)
       end.
#Fun

نصائح أخرى

Named funs are a implementation of EEP37 see the link for a detailed description and rationale.

This version doesn't need to pass in the function as one of the args:

1> Fac = fun(Num) -> Foo = fun(F, 0) -> 1; (F, N) when N > 0, is_integer(N) -> N * F(F, N -1) end, Foo(Foo, Num) end.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top