need an enhanced (for)

Pondering the philosophy behind the language
Locked
axtens
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

need an enhanced (for)

Post by 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
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Code: Select all

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

axtens
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

Post by axtens »

HPW wrote:

Code: Select all

(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
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

Post by axtens »

axtens wrote:
HPW wrote:

Code: Select all

(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:

Code: Select all

(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.

Code: Select all

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

Locked