From: Joerg Hoehle
Subject: legal LOOP (collect into x initially setq x y)?
Date: 
Message-ID: <uk6rv4coa.fsf@users.sourceforge.net>
Hi,

Is there some restriction on LOOP variables named via value
accumulation clauses? All I saw is mention of them being "bound as by
the construct with".

However, in both Iterate-1.0.9 and CLISP's LOOP, this does not
work as expected:
(loop initially (setq x (list 'a 'b)) ; try and prefill x
      for i in '(1 2 3)
      collect i into x
      finally (return x))
Is this portable code?

Background: Both Iterate and CLISP's loop keep an internal pointer on
the last cell so as to be able to efficiently COLLECT and NCONC. If I
modify the named variable, such code fails.

Is such last-cell optimization forbidden in ANSI-CL (unless it can be
proved that X is not written to)?

Regards,
	Jorg Hohle
Telekom/T-Systems Technology Center
From: Pascal Bourguignon
Subject: Re: legal LOOP (collect into x initially setq x y)?
Date: 
Message-ID: <87sm6j1hag.fsf@thalassa.informatimago.com>
Joerg Hoehle <······@users.sourceforge.net> writes:

> Hi,
> 
> Is there some restriction on LOOP variables named via value
> accumulation clauses? All I saw is mention of them being "bound as by
> the construct with".
> 
> However, in both Iterate-1.0.9 and CLISP's LOOP, this does not
> work as expected:
> (loop initially (setq x (list 'a 'b)) ; try and prefill x
>       for i in '(1 2 3)
>       collect i into x
>       finally (return x))
> Is this portable code?

At first I would have said no, but:

http://www.lispworks.com/reference/HyperSpec/Body/06_ac.htm

    * The append keyword causes its list values to be concatenated
      into a single list, as if they were arguments to the function
      append.

    * The nconc keyword causes its list values to be concatenated into
      a single list, as if they were arguments to the function nconc.

and:

    The var argument is bound as if by the construct with. 

So it seems that the collect/nconc-ing is done _as_if_ we walked the
list every time, so the above loop should work as you want everywhere.


> Background: Both Iterate and CLISP's loop keep an internal pointer on
> the last cell so as to be able to efficiently COLLECT and NCONC. If I
> modify the named variable, such code fails.
> 
> Is such last-cell optimization forbidden in ANSI-CL (unless it can be
> proved that X is not written to)?

I don't think it's forbidden. It just have to work _as_if_.  So this
optimization can be done, for example by the compiler, assuming that
loop generates an inlined version of append or nconc or the compiler
inlines automatically these functions. Or the loop macro can walk all
its bodies and generate the optimized code when it sees that the
accumulation variable is not modified by the user.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.