[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

next and continue with loop

Jim Newton

8/4/2015 8:32:00 AM

There are lots of clever things you can do with the LOOP macro.

A call to loop-finish will exit the loop but first execute the FINALLY clause if there is one.
If you want to exit the loop, bypassing the FINALLY clause you can use named/return-from

(loop :named this-loop
...
:when (some-condition)
:do (return-from this-loop)
...)


Does anyone know a clever way to "jump" to the next iteration.
What I normally do is put a (block ...) around the DO clause as follows.
But maybe there is a cleverer way.

(loop ...
:do (block this-iteration
...
(when (some-condition)
(return-from this-iteration)))
...)
10 Answers

RamRod Sword of Baal

6/5/2011 6:30:00 AM

0



"Strabo" wrote in message news:E5DGp.507$_I7.247@newsfe08.iad...

> People who refuse to follow Catholic laws and policy are not Catholics.

Well considering the majority of 'Catholic' married couples in most Western
countries do not follow the church's teaching on contraception, there cannot
be all that many Catholics then if we are to believe you.


Marvel

6/5/2011 9:01:00 AM

0


<ramrod@truthonly.com> wrote in message
news:VnFGp.5919$CS3.5239@viwinnwfe01.internal.bigpond.com...
>
>
> "Strabo" wrote in message news:E5DGp.507$_I7.247@newsfe08.iad...
>
>> People who refuse to follow Catholic laws and policy are not Catholics.
>
> Well considering the majority of 'Catholic' married couples in most
> Western countries do not follow the church's teaching on contraception,
> there cannot be all that many Catholics then if we are to believe you.
>
>

As you are not GOD and you are not Catholic, which qualifications you have
to judge Catholic families ?

N O N E !

Please avoid in the future to make village idiot from yourself in Christian
and Catholic groups.

We already have here 3 boring cretins trolling same idiocy here: Chilton,
Cholant and W.T.S.


RamRod Sword of Baal

6/5/2011 7:28:00 PM

0



"Strabo" wrote in message news:E5DGp.507$_I7.247@newsfe08.iad...


> People who refuse to follow Catholic laws and policy are not Catholics.


Read again what I have said about contraception in Catholic families

Marvel

6/5/2011 11:37:00 PM

0


<ramrod@truthonly.com> wrote in message
news:zKQGp.5955$CS3.3382@viwinnwfe01.internal.bigpond.com...
>
>
> "Marvel" wrote in message news:isfgju$94o$1@dont-email.me...
>
>
> <ramrod@truthonly.com> wrote in message
> news:VnFGp.5919$CS3.5239@viwinnwfe01.internal.bigpond.com...
>>
>>
>>> "Strabo" wrote in message news:E5DGp.507$_I7.247@newsfe08.iad...
>>
>>>> People who refuse to follow Catholic laws and policy are not Catholics.
>>
>
>> As you are not GOD and you are not Catholic, which qualifications you
>> have to judge Catholic families ?
>
>
>
> It seems that you made the judgment not I, when you said "People who
> refuse to follow Catholic laws and policy are not Catholics "
>
>

Indeed ! Same like for any other groups of people: Every froup has own rules
and if you are not playing according the rules you have to go.

So, PLEASE STOP CROSSPOSTING TO alt.religion.christian.roman-catholic and
alt.religion.christian

Nobody here want your abusing attacks on our beliefs, except extreme
trolling biggots (who are also intruders here just like you are).


Marvel

6/5/2011 11:38:00 PM

0


<ramrod@truthonly.com> wrote in message
news:WMQGp.5956$CS3.1136@viwinnwfe01.internal.bigpond.com...
>
>
> "Strabo" wrote in message news:E5DGp.507$_I7.247@newsfe08.iad...
>
>> People who refuse to follow Catholic laws and policy are not Catholics.
>
>
> Read again what I have said about contraception in Catholic families

As a Catholic, I am not concerned for your problems. Just go away.

PLEASE STOP CROSSPOSTING TO alt.religion.christian.roman-catholic and
alt.religion.christian


Nobody here want your abusing attacks on our beliefs, except extreme
trolling biggots (who are also intruders here just like you are).


Pascal J. Bourguignon

8/4/2015 9:19:00 AM

0

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

> There are lots of clever things you can do with the LOOP macro.
>
> A call to loop-finish will exit the loop but first execute the FINALLY clause if there is one.
> If you want to exit the loop, bypassing the FINALLY clause you can use named/return-from
>
> (loop :named this-loop
> ...
> :when (some-condition)
> :do (return-from this-loop)
> ...)
>
>
> Does anyone know a clever way to "jump" to the next iteration.
> What I normally do is put a (block ...) around the DO clause as follows.
> But maybe there is a cleverer way.
>
> (loop ...
> :do (block this-iteration
> ...
> (when (some-condition)
> (return-from this-iteration)))
> ...)

Notice that other loop operator have an implicit tagbody around the loop
body. So you can write code like:

(dolist (x '(1 2 3 4 5 6 7 8 9 10))
hello lisp! could you please
execute my loop very fast?
I'm in a hurry.
begin
(when (oddp x) (go end))
(print x)
(when (zerop (mod x 3))
(setf x (truncate x 3))
(go begin))
end
thank you.)
prints:
2
4
6
2
8
10
--> nil

(Of course, sbcl will complain about dead-code. Can you guess what it
is?)


But since :do bodies and other loop bodies only take compound forms
(non-atomic), you would need a tagbody, a block or some other
construct. Here, a nice operator is the PROG operator, which is a
LET + TAGBODY + LOCALLY all in one:

(loop
:for x :from 1 :below 10
:do (prog ((x x))
notice we need x rebound to avoid mutating the
loop variable below.
hello lisp! could you please
execute it very fast?
I'm in a hurry.
begin
(when (oddp x) (go end))
(print x) yeah! print it.
(when (zerop (mod x 3))
(setf x (truncate x 3))
(go begin)) and nowhere else
end
thank you.))
prints:
2
4
6
2
8
--> nil


Now, to see if you were following: what problem is there with writing
such "comments" in tagbodies?

--
__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

William James

8/4/2015 10:36:00 AM

0

Pascal J. Bourguignon wrote:

> > Does anyone know a clever way to "jump" to the next iteration.
> > What I normally do is put a (block ...) around the DO clause as follows.
> > But maybe there is a cleverer way.
> >
> > (loop ...
> > :do (block this-iteration
> > ...
> > (when (some-condition)
> > (return-from this-iteration)))
> > ...)
>
> Notice that other loop operator have an implicit tagbody around the loop
> body. So you can write code like:
>
> (dolist (x '(1 2 3 4 5 6 7 8 9 10))
> hello lisp! could you please
> execute my loop very fast?
> I'm in a hurry.
> begin
> (when (oddp x) (go end))
> (print x)
> (when (zerop (mod x 3))
> (setf x (truncate x 3))
> (go begin))
> end
> thank you.)
> prints:
> 2
> 4
> 6
> 2
> 8
> 10
> --> nil
>
> (Of course, sbcl will complain about dead-code. Can you guess what it
> is?)
>
>
> But since :do bodies and other loop bodies only take compound forms
> (non-atomic), you would need a tagbody, a block or some other
> construct. Here, a nice operator is the PROG operator, which is a
> LET + TAGBODY + LOCALLY all in one:
>
> (loop
> :for x :from 1 :below 10
> :do (prog ((x x))
> notice we need x rebound to avoid mutating the
> loop variable below.
> hello lisp! could you please
> execute it very fast?
> I'm in a hurry.
> begin
> (when (oddp x) (go end))
> (print x) yeah! print it.
> (when (zerop (mod x 3))
> (setf x (truncate x 3))
> (go begin)) and nowhere else
> end
> thank you.))
> prints:
> 2
> 4
> 6
> 2
> 8
> --> nil


Gauche Scheme:

(dolist (x (iota 10 1))
(while (and (even? x) (print x) (zero? (mod x 3)))
(set! x (/ x 3))))

===>
2
4
6
2
8
10
()


Paul Graham:

I consider Loop one of the worst flaws in CL, and an example
to be borne in mind by both macro writers and language designers.


[ In "ANSI Common Lisp", Graham makes the following comments: ]

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.
....
The first thing one notices about the loop macro is that it
has syntax. A loop expression contains not subexpressions but
clauses. The clauses are not delimited by parentheses;
instead, each kind has a distinct syntax. In that, loop
resembles traditional Algol-like languages. But the other
distinctive feature of loop, which makes it as unlike Algol as
Lisp, is that the order in which things happen is only
loosely related to the order in which the clauses occur.
....
For such reasons, the use of loop cannot be recommended.



Dan Weinreb, one of the designers of Common Lisp:

.... the problem with LOOP was that it turned out to be hard to
predict what it would do, when you started using a lot of
different facets of LOOP all together. This is a serious problem
since the whole idea of LOOP was to let you use many facets
together; if you're not doing that, LOOP is overkill.


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.


Barry Margolin:

> 3. Loop is very powerful, granted, and many people are trying to
> argue that "you can do so much with loop that it's unreadable."
> This is not an argument.

But it is! Because any use of LOOP has the potential to be
unreadable, the reader must read it carefully to verify that
it's just one of the cases that doesn't require careful
reading!


Barry Margolin (1997-01-08)

There are a few things that can be done extremely conveniently
with LOOP, and I will usually use LOOP in those cases. In
particular, the COLLECTING feature is one of the most
convenient.

The Generators and Collectors macros described in Appendix B
of CLtL2 also provide this convenience and are much more
Lisp-like. It's too bad they weren't around when LOOP was
gaining popularity, and I think they're a better way to go.
But LOOP is what I got used to, and its popularity is why it
got elevated into the ANSI standard.



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.


--
He has nothing but kind sentiments for those who would destroy his home and
family.... He is universally tolerant.... If he has any principles, he keeps
them well concealed.... He is, to the extent of his abilities, exactly like
the next citizen, who, he trusts, is trying to be exactly like him: a faceless,
characterless putty-man. --- Father Feeney; "Should Hate Be Outlawed?"

smh

8/7/2015 3:24:00 AM

0

There is no clever way to do a loop-continue to the next iteration. Superficially it;s an attractive thing to have, but there are good semantic and implementation reasons why it would be dangerous, and/or very hard to implement

The loop macro is complex, but most CL implementations have a loop that shares DNA from earlier implementations, so if you examine any open-source implementation you can explore with regard to my comments:

The loop macro analyzes all it's clauses and basically collects code to be inserted (each in semantic order) in these several places:

- variable initialization at loop entry
- end tests
- body forms
- iteration variable update
- final clauses/return values at loop exit

There are a lot of other details, but these are the basic Lisp code regions into which loop expands. It would seem attractive to provide a loop-continue macro (or function) that transfers to the variable update, but when there are multiple parallel iterations in a singe loop it is very difficult to keep these several code regions consistent.

I regret not being able to give a clearer description, or an implausible user-code example, but I've thought about this before and that was my conclusion. Loop is inelegant but not very pretty -- still, it is convenient.

If you are interested in pursuing this, I suggest using macroexpand of nontrivial loop forms to learn how they work, and what the difficulties would be. (Since loop often macroexpands into lower-level macro calls, it _might_ be necessary to use a real code walker instead of simple macroexpand. My favorite implementation has one, but not all make it available.) I don't want to put more time into this right now, but if you intend to engage the problem seriously I'd be willing to discuss further.

Madhu

8/7/2015 4:50:00 AM

0


The best way to do `next' or `continue' in a structured programming
language like CL's LOOP is like this:

(loop (cond ((termination-tests) ... (return)) ;; <- `return' ends the loop
(...) ...) ;; <-- control passes to the next loop
;; iteration, giving you `next' or
;; `continue' for free.
)

It is often worth taking the time to put your code into such a
structure, which the structured programming language encourages.

--- Madhu

William James

1/9/2016 9:12:00 PM

0

Jim Newton wrote:

> There are lots of clever things you can do with the LOOP macro.
>
> A call to loop-finish will exit the loop but first execute the FINALLY clause if there is one.
> If you want to exit the loop, bypassing the FINALLY clause you can use named/return-from
>
> (loop :named this-loop
> ...
> :when (some-condition)
> :do (return-from this-loop)
> ...)
>
>
> Does anyone know a clever way to "jump" to the next iteration.
> What I normally do is put a (block ...) around the DO clause as follows.
> But maybe there is a cleverer way.
>
> (loop ...
> :do (block this-iteration
> ...
> (when (some-condition)
> (return-from this-iteration)))
> ...)

MatzLisp (Ruby):

5.times{|i| next if i.odd?; puts i}
0
2
4
==>5
x=0; 3.times{|i| puts i; redo if (x += 1) == 1}
0
0
1
2
==>3

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...