Note 7

This was an answer to a Stack Overflow question that got closed.

(define-syntax nloop*
  ;; Nested numerical loop
  (syntax-rules ()
    [(_ () form ...)
     (begin form ...
            (values))]
    [(_ ((variable lower-inclusive upper-exclusive) more ...) form ...)
     (let loop ([variable lower-inclusive])
       (if (< variable upper-exclusive)
           (begin
             (nloop* (more ...) form ...)
             (loop (+ variable 1)))
           (values)))]
    [(_ ((variable start-inclusive end-exclusive step) more ...) form ...)
     (let ([cmp? (if (>= step 0) < >)])
       (let loop ([variable start-inclusive])
         (if (cmp? variable end-exclusive)
             (begin
               (nloop* (more ...) form ...)
               (loop (+ variable step)))
             (values))))]))
> (nloop* ((i 10 0 -3)
           (j 0 3 2))
    (printf "~A ~A~%" i j))
10 0
10 2
7 0
7 2
4 0
4 2
1 0
1 2

You'll only receive email when 100 suns publishes a new post

More from 100 suns