newbie q.
Thomas Arts
thomas@REDACTED
Thu Apr 13 08:40:03 CEST 2000
Kurt wrote:
>
> Greetings all.
> Given this:
>
> adder = fun(X) -> fun(Y) -> X + Y end end.
I guess you mean
Adder = fun(X) -> fun(Y) -> X + Y end end.
Since "adder" without a capital is an atom and in that sense
you try to match an atom with a function, which fails.
In case of "Adder" with a capital, Adder is a variable and
you try to match the variable with the function. If Adder has
not yet been asigned a value, this match is an assignment for
Adder to become placeholder for the function definition.
> I can do this :
> Adder3 = adder(3).
> and Adder3(10) returns 13.
This match is equivalent to
Adder3 = fun(Y) -> 3 + Y end.
> However I can also do this:
> AdderX = adder(Adder3).
which is equivalent to
AdderX = fun(Y) -> (fun(Z) -> 3+Z end) + Y end.
> So, how can I call AdderX ? Is it possible?
sure you can call this AdderX and it will give exactly the
right result:
Error: badarith
AdderX(5) results in an attempt to add 5 to a function.
This is not possible, hence the error message.
> When I do AdderX(5), is this trying to do: Adder3 + 5
> which is an error, but is syntactically correct?
Sure, the syntax permits more than what is computational
possible. As in most languages. The syntax for example allows
to write 2/0, but most languages give up on this.
It's not really the nicest type error you could hope for, but you
are at least warned for doing something strange instead of
having a runtime system that assumes the value to be 0 and continues
computation ;0).
/Thomas
More information about the erlang-questions
mailing list