fun({0, M}) -> {M+1, M-2};
fun({N, M}) ->
{A, B} = fun({N-1, M+1}),
{B, A+1}.
so I am kinda unsure of what the A and B would be and how the next recursive call would be. let say 2,2
so it would be
f(2,2) -> {A,B} = fun({1,3}), {B,A+1}
f(1,3) -> {A,B} = fun({0,4}), {B,A+1}
f(0,4) -> {5,2}
but where does A and B go and do they change in each recursive call?
You just need to go back up:
(note that
fun
is a keyword in Erlang and thatf(N,M)
is not the same asf({N,M})
)Yes, as you can see.
As a very basic explanation of "where is my variable", consider the countdown function in this example module:
If we hit the base case, which is
0
, then we stop. The return value of the function overall is the atomok
(because that is the return value of a successful call toio:format/2
).If the input is greater than
0
then we match on the second clause, which means we assignN
the sole input argument for this particular iteration. The next thing we do is make our output call. Then we assignNext
to the valueN - 1
. Then we call the same function again (do a loop) using the value ofNext
in body of the the current call as the input argument.The next iteration all the variables are totally new because this is a fresh execution context. The old
N
andNext
no longer exist. In fact, they don't event exist on a stack anywhere because Erlang uses "tail call optimization" to maintain recursive tail calls in constant space, the same way most other languages would do an explicitfor
orwhile
ordo while
or [insert form].As Alexy points out, be careful about the token
fun
-- it is a keyword in Erlang, not a legal function name. It is the non-name of an anonymous function (also known as a lambda). In other words, unless you provide a label, the name of every anonymous function is justfun
.fun
is also the keyword that is used to reference a function by label (to use as a value itself) instead of calling it. For example,countdown(10)
calls the function above with an argument of10
. Referencing the function asfun countdown/1
returns the function itself as a value. That is, incidentally, why the function export declaration at the top of the module is written as-module([countdown/1])
, because that is the explicit name of this function. Consider this:While I'm on the subject...
Erlang has very few keywords compared to most languages (and very little syntax, actually). Here is the list of reserved words: