[erlang-questions] Function Declaration Syntax

Richard O'Keefe ok@REDACTED
Mon May 16 05:58:36 CEST 2011


On 14/05/2011, at 3:01 AM, Attila Rajmund Nohl wrote:

> The (usual) problem is that this is not consistent. You have to write
> the function name for different function clauses, but must not write
> the 'fun' for different clauses in anonymous functions. I would prefer
> the 'fun' anu anonymous functions too.

But that's because "fun" isn't the anonymous function's name.
The name of the anonymous function *is* repeated in every clause.
That name is the sequence of characters strictly between these
brackets: [].

fun  (........) -> .......
    ^ name
  ;  (........) -> .......
    ^ exactly the same name again
end.

You may not *like* the name being invisible, especially because that
makes it impossible to write recursive funs, but you can't really
call it inconsistent (:-) (:-).

An interesting alternative would be to allow

    fun F(N) when is_integer(N), N > 1 -> F(N-1) + F(N-2)
      ; F(1) -> 1
      ; F(0) -> 1
    end

and if the variable here were optional (either appearing in all
clauses or disappearing in all clauses) it would be clear that the
traditional form has a consistently repeated but invisible name.

The thing that makes funs look ugly is people putting the semicolons
in the wrong place...




More information about the erlang-questions mailing list