[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

loop question FOR vs WITH

Jim Newton

8/4/2015 8:35:00 AM

Does anyone know if there is a difference between FOR/= and WITH/= in a LOOP such as the following?

(loop :for x :in data
:for y := (f x)
:do ...)

(loop :for x :in data
:with y := (f x)
:do ...)

7 Answers

Marco Antoniotti

8/4/2015 9:02:00 AM

0

On Tuesday, August 4, 2015 at 11:35:18 AM UTC+3, Jim Newton wrote:
> Does anyone know if there is a difference between FOR/= and WITH/= in a LOOP such as the following?
>
> (loop :for x :in data
> :for y := (f x)
> :do ...)
>
> (loop :for x :in data
> :with y := (f x)
> :do ...)

WITH variable initialization should be done first. The y in your second example is initialized only once (as per ANSI).

Cfr. (In LW)

CL-USER 3 > (loop for i from 0 to 5
with j = (1+ i)
do (print i) (print j))
Warning: Local Variable Initialization clause (the binding of J) follows iteration forms but will be evaluated before them.
0
1
1
1
2
1
3
1
4
1
5
1
NIL



Cheers
--
MA

Marco Antoniotti

8/4/2015 9:03:00 AM

0

On Tuesday, August 4, 2015 at 11:35:18 AM UTC+3, Jim Newton wrote:
> Does anyone know if there is a difference between FOR/= and WITH/= in a LOOP such as the following?
>
> (loop :for x :in data
> :for y := (f x)
> :do ...)
>
> (loop :for x :in data
> :with y := (f x)
> :do ...)

Of course, the above is a WJ bait :) But we all realize that :)

Cheers
--
MA

Pascal J. Bourguignon

8/4/2015 9:05:00 AM

0

Jim Newton <jimka.issy@gmail.com> writes:

> Does anyone know if there is a difference between FOR/= and WITH/= in a LOOP such as the following?
>
> (loop :for x :in data
> :for y := (f x)
> :do ...)
>
> (loop :for x :in data
> :with y := (f x)
> :do ...)
>

(loop :with y := (f x)
:for x :in data
:do â?¦)

is equivalent to

(let ((y (f x)))
(loop :for x :in data
:do â?¦))

It's bad style to write the :with clause after the :for, some
implementations will complain.

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Espen Vestre

8/4/2015 9:08:00 AM

0

Jim Newton <jimka.issy@gmail.com> writes:

> Does anyone know if there is a difference between FOR/= and WITH/= in
> a LOOP such as the following?

WITH is just like a LET*, or as the bible says: "The with construct
initializes variables that are local to a loop. The variables are
initialized one time only."

So:

CL-USER 6 > (loop for i from 1 to 3
for j = (expt i 2)
with k = (expt i 2)
collect (list i j k))
((1 1 1) (2 4 1) (3 9 1))

--
(espen)

Kaz Kylheku

8/4/2015 3:14:00 PM

0

On 2015-08-04, Jim Newton <jimka.issy@gmail.com> wrote:
> Does anyone know if there is a difference between FOR/= and WITH/= in a LOOP such as the following?
>
> (loop :for x :in data
> :for y := (f x)
> :do ...)
>
> (loop :for x :in data
> :with y := (f x)
> :do ...)

"With" is static: it evaluates the initializing expression once; it is loop's
equivalent of "let".

"For" evaluates the initializing expression on each initialization.

If f has no side effects and x doesn't change, then they are the same.
(Of course, x does change above, so they aren't.)

The for clause also has more syntax attached to it. As you can see you have
the "in" keyword. There is no "with y in data" syntax.

"for = " also has more syntax attached to it, namely a "then clause":

for y = (f x) then (1+ x)

(f x) is the initial value for the first iteration, (1+ x) gives the new
value on subsequent iterations.

Multiple for clauses can combine with "and", which achieves parallel binding.

for x = a then c and
for y = b then d

means that x y take on a b simultaneously, and then c d simultaneously.
If expressions c and d refer to the variables x and y, they both refer
to the previous values. If a and b refer to x and y, they will find those
variables nil.

Without the and, expressions b and d are exposed to the new value of x
stored by the prior for.

Lastly, that brings up scope. I am not sure whether this is true in every
loop implementation, but if you do

with x = x

and there is no binding for x in scope, it is an error. This allows:

(let ((x 42))
(loop with x = x ;; loop's x is initialized to 42
....))

However in

for x = x

the x expression is already under the scope of the x on the left hand side;
it is self-assignment.

Teemu Likonen

8/4/2015 5:33:00 PM

0

Kaz Kylheku [2015-08-04 15:14:20Z] wrote:

> Multiple for clauses can combine with "and", which achieves parallel
> binding.
>
> for x = a then c and
> for y = b then d

Correction:

for x = a then c
and y = b then d

The relevant syntax:

for-as-clause::= {for | as} for-as-subclause {and for-as-subclause}*

for-as-subclause::= for-as-arithmetic | for-as-in-list |
for-as-on-list | for-as-equals-then | for-as-across |
for-as-hash | for-as-package

http://www.lispworks.com/documentation/HyperSpec/Body/...

William James

8/4/2015 8:36:00 PM

0

Teemu Likonen wrote:

> Kaz Kylheku [2015-08-04 15:14:20Z] wrote:
>
> > Multiple for clauses can combine with "and", which achieves parallel
> > binding.
> >
> > for x = a then c and
> > for y = b then d
>
> Correction:
>
> for x = a then c
> and y = b then d
>
> The relevant syntax:
>
> for-as-clause::= {for | as} for-as-subclause {and for-as-subclause}*
>
> for-as-subclause::= for-as-arithmetic | for-as-in-list |
> for-as-on-list | for-as-equals-then | for-as-across |
> for-as-hash | for-as-package

In "ANSI Common Lisp", Graham makes the following comment:

The loop macro was originally designed to help inexperienced
Lisp users write iterative code. Instead of writing Lisp code,
you express your program in a form meant to resemble English,
and this is then translated into Lisp. Unfortunately, loop is
more like English than its designers ever intended: you can
use it in simple cases without quite understanding how it
works, but to understand it in the abstract is almost
impossible.
....
the ANSI standard does not really give a formal specification
of its behavior.



Barry Margolin, 05 Apr 2001
(http://groups.google.com/group/comp.lang.lisp/msg/8a48ce...)

>(My second rule of thumb concerning LOOP would be the negative of
>Barry Margolin's: The more complex the looping, the more you need/want
>to use LOOP.)

My recommendation is based on seeing many question in the past of the form
"What happens if you use both XXX and YYY in the same LOOP?" The
unfortunate fact is that when we were writing the standard we didn't have
time to nail down all the possible interactions between different LOOP
features, so many of these are not well specified. And even if we did get
it right in the standard, it's likely to be difficult to find them and I
wouldn't trust that all implementors got it right (many of those questions
were probably from implementors, trying to figure out what they were
supposed to do). And even if they all got it right, someone reading your
code may not be able to figure it out.

So, with all those potential problems, my feeling is that if you have to
ask, it's probably better to use something other than LOOP.


From: John Foderaro <jkf@unspamx.franz.com>
Newsgroups: comp.lang.lisp
Subject: Re: the "loop" macro
Date: Sun, 26 Aug 2001 10:51:26 -0700

I'm not trying to join a debate on loop. I just wanted to present
the other side of [the issue so that] the intelligent people can
then weigh the arguments on both sides.

I'm not suggesting that loop can be fixed either by adding
parenthesis or coming up with ways of indenting it to make it
understandable. It's a lost cause.

--
Africans gang-rape and clitorectomize Finnish girl; government arrests Finn
whom they accuse of complaining:
conservative-headlines.com/2009/03/another-european-awaits-extradition-for-hate-speech