3 ms·
Clojure's loop/recur is pretty much a degenerate version of Scheme's named let, which has been part of Scheme since long before Clojure was a thing: (define
by groovy2shoes 10y ago
Clojure's loop/recur is pretty much a degenerate version of Scheme's named let, which has been part of Scheme since long before Clojure was a thing:
(define (sum xs)
(let loop ((acc 0)
(xs xs))
(if (null? xs)
acc
(loop (+ acc (car xs)) (cdr xs)))))
It's often used in Scheme for iteration, including cases where you would otherwise need to define a helper function to implement an iterative (tail-recursive) version of a function, as I did with sum above.
The named let construct is more general than loop/recur, since you can supply a name and properly nest the forms.