Here's a straightforward translation to a do
loop:
(define (foo x y z)
(do ((result 0 (+ result i))
(i x (+ i z)))
((>= i y) result)))
However, many Schemers find do
loops to be distasteful. So here's an identical loop that uses named let
, which is in fact what the do
is likely to expand to:
(define (foo x y z)
(let loop ((result 0)
(i x))
(if (>= i y)
result
(loop (+ result i) (+ i z)))))
which is likely to expand to:
(define (foo x y z)
((rec (loop result i)
(if (>= i y)
result
(loop (+ result i) (+ i z))))
0 x))
which then expands to:
(define (foo x y z)
((letrec ((loop (lambda (result i)
(if (>= i y)
result
(loop (+ result i) (+ i z))))))
loop)
0 x))
Yay macros! Choose the version you like best; most Schemers I know prefer the second one.