(module dotimes mzscheme
(provide dotimes)
(define-syntax (dotimes stx)
(syntax-case stx ()
[(dotimes (var count-form finally) body ...)
#`(let ([count count-form])
(unless (integer? count)
(raise-syntax-error 'dotimes
(format "expected integer as result of expression, got ~s " count)
#'count-form))
(if (positive? count)
(let ([var 0])
(let loop ([i 0])
(set! var i)
(if (< i count)
(begin
body ...
(loop (add1 i)))
finally)))
(let ([var 0])
finally)))]
[(dotimes (var count-form) body ...)
#'(dotimes (var count-form (void)) body ...)]
[else
(raise-syntax-error #f "bad syntax, (dotimes (var expr [finally-expr]) body ...) expected" stx)]))
)