문제

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