[plt-scheme] Outer-inner loops in Scheme?

From: Will M Farr (farr at MIT.EDU)
Date: Mon May 14 09:19:29 EDT 2007


You're probably teaching a class or something where you don't want  
the extra complication of introducing SRFI's, but here's how I would  
do it:

(require (lib "42.ss" "srfi"))

(define (do-it x-max y-max)
   (do-ec (:range x x-max) (:range y y-max)
     (printf "~a ~a~n" x y)))

(do-it 10 10)

(Note that SRFI-42's :range generator generates all values *less  
than* x-max and y-max, so I have to loop *to* 10 while you looped  
*through* 9.)

It's rare that I ever write loops myself these days.


On May 14, 2007, at 9:06 AM, Grant Rettke wrote:

> Hi folks,
> Last night I looked at a solution to a programming problem and the
> typical approach was to use an inner and outer loop (think C or Ruby).
> My approach in Scheme was to do something like the following:
> (define test
>  (λ ()
>    (outer-inner-loop 0 0 9 9)))
> (define outer-inner-loop
>  (λ (x y x-max y-max)
>    (begin
>      (printf "~a ~a~n" x y)
>      (cond [(and (>= x x-max) (>= y y-max)) 'done]
>            [(< y y-max) (outer-inner-loop x (add1 y) x-max y-max)]
>            [else (outer-inner-loop (add1 x) 0 x-max y-max)]))))
> (test)
> Is this the typical way you would implement "outer-inner looping"  
> in Scheme?
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.