In a recursive loop, I would like to change the value of a variable:
loop(N) when N > ... ->
N;
loop(N) ->
case ... of
N+1
...
end,
...
case ... of
N-1
...
end,
...
loop(N).
How to "pass" the new value of N ?
In a recursive loop, I would like to change the value of a variable:
loop(N) when N > ... ->
N;
loop(N) ->
case ... of
N+1
...
end,
...
case ... of
N-1
...
end,
...
loop(N).
How to "pass" the new value of N ?
Since you can't change the value of
N
once assigned, you need to callloop
with the new value:Alternatively, create a temporary variable to hold its new value before passing it into the recursive
loop
invocation.If this results in a lot of repetition in your code, you might want to separate the looping construct from the logic which produces the new value of N:
You simply call the function with a new value:
Shamelessly stolen from: http://learnyousomeerlang.com/recursion#hello-recursion