need an enhanced (for)

Started by axtens, April 12, 2009, 02:49:44 AM

Previous topic - Next topic

axtens

G'day everyone



I'm not sure how I'd handle this, as I'm just thinking about it now, but how would one implement a (for) that had two indices rather than the usual one?



For example (pseudopseudocode):

  for i = 1 to 10 with j=20 to 2 step -2 do

    println i,j

  next



The usual thing is to have the for j inside the for i and to get about 100 results. I was thinking more of getting as many results as it takes for either i or j to terminate. That is, if i gets to 10 before j gets to 2 then terminate or vice versa.



I may come up with something soon, but if anyone wants to offer a solution, I'd be very interested.



Kind regards,

Bruce.

HPW

#1

(setq j 20)
(for (i 1 10 1 (= j 0))
 (println (string "i=" i "j=" j))
 (setq j (- j 2))
)
Hans-Peter

axtens

#2
Quote from: "HPW"
(setq j 20)
(for (i 1 10 1 (= j 0))
 (println (string "i=" i "j=" j))
 (setq j (- j 2))
)


Wow, would never of thought of that approach. Thanks!!



Kind regards,

Bruce.

axtens

#3
Quote from: "axtens"
Quote from: "HPW"
(setq j 20)
(for (i 1 10 1 (= j 0))
 (println (string "i=" i "j=" j))
 (setq j (- j 2))
)


However, what I ended up doing was:


(setq i ?TestRange1Low)
(setq j ?TestRange2Low)
(while (and (!= i ?TestRange1High) (!= j ?TestRange2High))
(begin
(inc i ?TestIncr1)
(inc j ?TestIncr2)
)
)


which, whilst not brilliant, does work. I suppose what I was thinking of was some new kind of syntax that would enable me to set up a co-enumeration, e.g.

   (co-for (i 1 10 1) (j 20 2 -2) (begin ... ) )


--Bruce