re: Two DO questions

Liste des GroupesRevenir à cl scheme 
Sujet : re: Two DO questions
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 08. Jul 2025, 16:52:17
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <104jerg$3lbvm$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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)

Date Sujet#  Auteur
8 Jul16:52 o Re: Two DO questions1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal