I have no documentation for the ANSI Loop macro but I thought it
would would behave as follows :
> (loop for i from 1 to 5
for j from 1 to 2 do collect i)
=> (1 1 2 2 3 3 4 4 5 5)
but instead it returns (1 2)
and
> (loop for i from 1 to 5 do
(loop for j from 1 to 2 do collect i))
returns nil
How do I get it to do what I want?
Software is AKCL and cmu-loop
--
Bobby Abraham
Computer Science
University of Natal, Pietermaritzburg
email: ········@unpcs1.cs.unp.ac.za
In article <··················@unpcs1> ········@unpcs1.cs.unp.ac.za (Bobby Abraham) writes:
>I have no documentation for the ANSI Loop macro
It's documented in CLtL2.
> but I thought it
>would would behave as follows :
>
>> (loop for i from 1 to 5
> for j from 1 to 2 do collect i)
>
>=> (1 1 2 2 3 3 4 4 5 5)
>
>but instead it returns (1 2)
All the iteration steps are done in parallel, not nested. The loop ends
as soon as one of the iterations ends.
>and
>> (loop for i from 1 to 5 do
> (loop for j from 1 to 2 do collect i))
>
>returns nil
COLLECT only causes the collection to be returned from the immediately
containing loop, but you're not doing anything with that return value.
>How do I get it to do what I want?
(loop for i from 1 to 5 nconcing
(loop for j from 1 to 2 do collect i))
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar
········@unpcs1.cs.unp.ac.za (Bobby Abraham) writes:
> I have no documentation for the ANSI Loop macro but I thought it
> would would behave as follows :
(loop for i from 1 to 5
for j from 1 to 2 do collect i)
=> (1 1 2 2 3 3 4 4 5 5)
> but instead it returns (1 2)
(loop for i from 1 to 5 do
(loop for j from 1 to 2 do collect i))
=> NIL
> How do I get it to do what I want?
I'm not quite sure what you're trying to do (other than get the
desired list as a result). In your second example with the nested for
loop you are collecting the list (i i) on each iteration of the inner
loop but not doing anything to collect it in the outer loop. Try
> (loop for i from 1 to 5 append
(loop for j from 1 to 2 collect i))
(1 1 2 2 3 3 4 4 5 5)
If you don't need the nested control structure, you could try
> (loop for i from 1 to 5 append (list i i))
(1 1 2 2 3 3 4 4 5 5)
--
Brent Benson ···@cs.unh.edu
Department of Computer Science 603-862-3786
University of New Hampshire
Durham, NH 03824