• re: Two DO questions

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Tue Jul 8 15:52:17 2025
    From Newsgroup: comp.lang.lisp

    Jules Grosse wrote:

    1) How can I reproduce the following behaviour:

    (dotimes (x 10)
    (dotimes (y 10)
    (do-something-with x y)))

    Using only one "do" construct?


    Kenny Tilton wrote:

    (do ((x 0 (if (= y 9)
    (incf x)
    x))
    (y 0 (if (= y 9)
    0 (incf y))))
    ((>= x 9))
    (print (list x y)))

    In "((>= x 9))", the >= should have been >.

    Testing:

    (dotimes (x 3)
    (dotimes (y 3)
    (print (list x y))))

    (0 0)
    (0 1)
    (0 2)
    (1 0)
    (1 1)
    (1 2)
    (2 0)
    (2 1)
    (2 2)

    (do ((x 0 (if (= y 2)
    (incf x)
    x))
    (y 0 (if (= y 2)
    0 (incf y))))
    ((>= x 2))
    (print (list x y)))

    (0 0)
    (0 1)
    (0 2)
    (1 0)
    (1 1)
    (1 2)

    The right way: [Tested with Gauche Scheme and SBCL.]

    (do ((y 0 (if (= 2 y) 0 (+ 1 y)))
    (x 0 (+ x (if (= 2 y) 1 0))))
    ((> x 2))
    (print (list x y)))

    (0 0)
    (0 1)
    (0 2)
    (1 0)
    (1 1)
    (1 2)
    (2 0)
    (2 1)
    (2 2)
    --- Synchronet 3.21a-Linux NewsLink 1.2