4 ms·
You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions. Here is an example of using a recursiv
by testvox 9y ago
You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions.
Here is an example of using a recursive function in Erlang to sum up a factorial.
fac(1) ->
1;
fac(N) ->
N * fac(N - 1).
- maxxxxx 9y agoDoes Erlang handle the stack better? In most languages this would blow up if N got big enough.
- bearhall 9y agoYes. Erlang has tail recursion which helps prevent that http://learnyousomeerlang.com/recursion http://learnyousomeerlang.com/recursion
- dragonwriter 9y agoAs the article notes, what Erlang has is Last Call Optimization, this is significantly more general than tail recursion optimization.