Express loops as named let or mutually recursive procedures whose recursive calls are in documented tail positions.
At a glance
LISPEX
(let loop ((xs (list 1 2 3 4)) (sum 0)) (if (null? xs) sum (loop (cdr xs) (+ sum (car xs)))))Result
OUTPUT
10Working method
- Carry accumulators as parameters instead of combining work after the recursive return.
- Tail position survives final
if,begin,and/or,cond,case,do,call-with-values, andapplypaths. - Use a bounded input during testing and distinguish continuation depth from runtime cost.
Boundaries
- Tail safety does not make an infinite loop terminate or remove resource budgets.