range - Idiomatic Nested looping in racket/scheme -


has got idea idiomatic method nested looping on numbers within range in racket/scheme? in python have:

for in range(numb1):     j in range(numb2): 

what equivalent in racket/scheme?

in racket it's simple this, using iterations , comprehensions:

(for* ([i (in-range numb1)]        [j (in-range numb2)])   <body of iteration>) 

the above work in racket. comparison, following snippets work in standard rxrs interpreter - instance, using pair of nested do:

(do ((i 0 (+ 1))) ((= numb1))   (do ((j 0 (+ j 1))) ((= j numb2))     <body of iteration>)) 

yet option: using explicit recursion , named let:

(let loop1 ((i 0))   (cond ((< numb1)          (let loop2 ((j 0))            (cond ((< j numb2)                   <body of iteration>                   (loop2 (+ j 1)))))          (loop1 (+ 1))))) 

finally, can following, refer sicp under section "nested mappings" details:

(define (range start stop)   (let loop ((i (- stop 1))              (acc '()))     (if (< start)         acc         (loop (- 1) (cons acc)))))  (for-each  (lambda (i)    (for-each     (lambda (j)       <body of iteration>)     (range 0 numb2)))  (range 0 numb1)) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -