Kata:
Training on Growth of a Population | Codewars
recur
loop
takes a special form that lets you declare some mutable state.
recur
will simply update that state in the order it was declared in loop
.
Since recur is recursive and not iterative, this is effectively a while loop, where loop
lets us track accumulated state.
(loop [a 0
b 1]
(if (>= a 10) a ; return a value else recur
(recur (+ b 1) ; -> a is now (+ b 1)
(+ a 2)))) ; -> b is now (+ a 2)
; |> updates in param order of loop
What is the difference between loop/recur and recur by itself?
loop allows you to accept and pass arbitrary parameters. Without loop you would be limited with only being able to pass only what function accepts. It would lead to heaps of tiny auxiliary functions