From: Dan Bensen
Subject: continuing next iteration
Date: 
Message-ID: <edcha1$qdu$1@wildfire.prairienet.org>
Trying to skip the rest of the body inside a loop.  Is there nothing 
like "continue" in C or "next" in Perl?  All I could find was either go 
to a tag at the end of the body or return from a block or function 
wrapping the body. Are those all the options?

-- 
My name is dsb, and I'm at prairienet, which is an O-R-G.

From: Tayssir John Gabbour
Subject: Re: continuing next iteration
Date: 
Message-ID: <1157223933.657620.77780@i42g2000cwa.googlegroups.com>
Dan Bensen wrote:
> Trying to skip the rest of the body inside a loop.  Is there nothing
> like "continue" in C or "next" in Perl?

Surprisingly, Loop doesn't. Iterate apparently does [1]. The DO family
(dotimes, etc)  has an implicit tagbody, so I guess you could GO to a
label at the end... you could also use BLOCK.

Googling usenet for "loop continue" comes up with some discussion:
http://groups.google.com/group/comp.lang.lisp/search?group=comp.lang.lisp&q=loop+continue&qt_g=1&searchnow=Search+this+group

At least as far as the standard goes, Loop's limits are glaring
(because Loop doesn't normally seem limited); like how you can't mix up
things like FOR and DO/COLLECT clauses.


[1] - I Looked at the Iterate manual, and it has next-iteration. Some
say it's a better Loop, and I wouldn't be surprised if it's true, but I
like Loop's unlispiness...
http://common-lisp.net/project/iterate/#examples


Tayssir
From: Rob Warnock
Subject: Re: continuing next iteration
Date: 
Message-ID: <ZbadnQYo4IwQ0GfZnZ2dnUVZ_omdnZ2d@speakeasy.net>
Tayssir John Gabbour <···········@yahoo.com> wrote:
+---------------
| At least as far as the standard goes, Loop's limits are glaring
| (because Loop doesn't normally seem limited); like how you can't
| mix up things like FOR and DO/COLLECT clauses.
+---------------

Hunh? I do stuff like that all the time, e.g.:

    > (loop for i below 6
	    finally (return (reverse (pairlis list list2)))
	do (format t "~d" i)
	collect i into list
	do (format t "^2 = ~2d~%" (* i i))
	collect (* i i) into list2)
    0^2 =  0
    1^2 =  1
    2^2 =  4
    3^2 =  9
    4^2 = 16
    5^2 = 25
    ((0 . 0) (1 . 1) (2 . 4) (3 . 9) (4 . 16) (5 . 25))
    > 

Or are you talking about all the FORs having to come before
all the DO/COLLECT/SUM/etc.? Well, C's no better in that regard...


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Tayssir John Gabbour
Subject: Re: continuing next iteration
Date: 
Message-ID: <1157270681.817307.42220@p79g2000cwp.googlegroups.com>
Rob Warnock wrote:
> Tayssir John Gabbour <···········@yahoo.com> wrote:
> +---------------
> | At least as far as the standard goes, Loop's limits are glaring
> | (because Loop doesn't normally seem limited); like how you can't
> | mix up things like FOR and DO/COLLECT clauses.
> +---------------
>
> Hunh? I do stuff like that all the time, e.g.:
>
>     > (loop for i below 6
> 	    finally (return (reverse (pairlis list list2)))
> 	do (format t "~d" i)
> 	collect i into list
> 	do (format t "^2 = ~2d~%" (* i i))
> 	collect (* i i) into list2)

I don't mean that, but I should use examples when talking about Loop...

;; good
(loop for ...
      collect ...)

;; incorrect
;; http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm
(loop collect ...
      for ...)


> Or are you talking about all the FORs having to come before
> all the DO/COLLECT/SUM/etc.? Well, C's no better in that regard...

Yup, and there are no doubt codebases which have that error [1]. Maybe
C's no better in that regard, but... I don't expect C to be. ;)

Another design decision I bump up against is Loop's limited
destructuring bind. Sucks when using plists... Apparently again ITERATE
seems better in this regard, and I suppose I should adopt it on the
rare occasions when a Loop isn't expressive enough. (But it would
probably kill something within me to stop using Loop.)

;; Won't do what you might expect -- &key will
;; be treated like a normal var, rather than
;; interpreted by something like destructuring-bind.
(loop for (&key p1 p2 p3) ...
      ...)


[1] - Apparently CL-HTTP had it all over the place. I suspect a good
addition to the spec is to redefine loop to allow this portably; maybe
I haven't thought about it enough, but I'd be impressed to know if this
seriously breaks old code.
http://groups.google.com/group/comp.lang.lisp/msg/32f44742dadde19f


Tayssir

--
"Patriotism is usually the refuge of the scoundrel. He is the man who
talks the loudest."
- Mark Twain
From: Tayssir John Gabbour
Subject: Re: continuing next iteration
Date: 
Message-ID: <1157279122.380923.147720@74g2000cwt.googlegroups.com>
Tayssir John Gabbour wrote:
> Another design decision I bump up against is Loop's limited
> destructuring bind. Sucks when using plists... Apparently again ITERATE
> seems better in this regard, and I suppose I should adopt it on the
> rare occasions when a Loop isn't expressive enough. (But it would
> probably kill something within me to stop using Loop.)
>
> ;; Won't do what you might expect -- &key will
> ;; be treated like a normal var, rather than
> ;; interpreted by something like destructuring-bind.
> (loop for (&key p1 p2 p3) ...
>       ...)

Yeah, and given that Iterate code-walks, so you apparently can do more
general things than destructuring-bind... like with-slots... sigh, I'll
really have to look into Iterate. I'm bumping up against Loop's
limitations frequently now... not to mention this may save me from
having to modify Emacs to indent Loop just the way I want.


Tayssir


--
"Patriotism is usually the refuge of the scoundrel. He is the man who
talks the loudest."
- Mark Twain
From: Novus
Subject: Re: continuing next iteration
Date: 
Message-ID: <2006090419594216807-novus@ngoqdeorg>
On 2006-09-03 06:25:22 -0400, "Tayssir John Gabbour" 
<···········@yahoo.com> said:

> Yeah, and given that Iterate code-walks, so you apparently can do more
> general things than destructuring-bind... like with-slots... sigh, I'll
> really have to look into Iterate. I'm bumping up against Loop's
> limitations frequently now... not to mention this may save me from
> having to modify Emacs to indent Loop just the way I want.

Iterate is awesome. It is what loop should have been IMHO.

Novus
From: Dan Bensen
Subject: Re: continuing next iteration
Date: 
Message-ID: <edchs9$qja$1@wildfire.prairienet.org>
Dan Bensen wrote:
> Trying to skip the rest of the body inside a loop. 

Not (necessarily) the "loop" function literally.
I'm using "do", "dotimes", etc..

-- 
My name is dsb, and I'm at prairienet, which is an O-R-G.
From: ········@gmail.com
Subject: Re: continuing next iteration
Date: 
Message-ID: <1157248990.115412.242950@b28g2000cwb.googlegroups.com>
Dan Bensen wrote:
> Dan Bensen wrote:
> > Trying to skip the rest of the body inside a loop.
>
> Not (necessarily) the "loop" function literally.
> I'm using "do", "dotimes", etc..

not only do the DO/DO*/DOLIST/DOTIMES family wrap ,@body in an implicit
block NIL, they wrap it in an implicit TAGBODY so you can use GO within
them...

> 
> -- 
> My name is dsb, and I'm at prairienet, which is an O-R-G.
From: Pascal Bourguignon
Subject: Re: continuing next iteration
Date: 
Message-ID: <877j0motzd.fsf@thalassa.informatimago.com>
Dan Bensen <··········@cyberspace.net> writes:

> Dan Bensen wrote:
>> Trying to skip the rest of the body inside a loop. 
>
> Not (necessarily) the "loop" function literally.
> I'm using "do", "dotimes", etc..


You can use BLOCK and RETURN/RETURN-FROM.  (This correspond to C break).

When you read  the CLHS page about a macro, notice careful whether it
generates a BLOCk and what name it uses.

For example, DO and DO*:

    An implicit block named nil surrounds the entire do (or do*)
    form. A return statement may be used at any point to exit the loop
    immediately.

(do ((i 0 (1+ i)))
    ((> i 10) :done)
 (if (> i 4) (return :interrupted)))


(do ((i 0 (1+ i)))
    ((> i 10) :done)
 (if (> i 4) (return :interrupted)))

--> :INTERRUPTED


With complex LOOP, you can use LOOP-FINISH:

(let ((i 0)) 
  (loop :do
     (prin1 (incf i)) (princ " ")
     (cond ((evenp i) (terpri))
           ((< 6 i) (loop-finish)))))
1 2 
3 4 
5 6 
7 
--> NIL

You can just name the loop:

  (loop :named this-loop
        :for i :from 0 :to 10
        :do (if (< 6 i) (return-from this-loop :interrupted))
        :finally (return-from this-loop :normal-exit))

But if you don't name it, it still generates a BLOCK with NIL as name,
so you can use RETURN:

  (loop :for i :from 0 :to 10
        :do (if (< 6 i) (return :interrupted))
        :finally (return :normal-exit))




For the corresponding of C continue, you can often easily just use IF
or COND.  But if you have a more complex loop body structure, you can
also use an explicit BLOCK:


(loop :named break
      :for i :from 0 :to 10
      :do (block continue
             (prin1 i) (princ " ")
             (cond ((oddp i) (return-from continue))
                   ((< 5 i)  (return-from break)))
             (princ "-- ")))
0 -- 1 2 -- 3 4 -- 5 6 
--> NIL


If you really long for C control structures, you can write macros:

(defmacro while (condition &body body)
   (let ((break (gensym))
         (continue (gensym)))
     `(loop :named ,break
            :while ,condition
            :do (block ,continue
                  (flet ((break    () (return-from ,break))
                         (continue () (return-from ,continue)))
                      ,@body)))))

(let ((i 0))
  (while (< (incf i) 10)
    (prin1 i) (princ " ")
    (cond ((oddp i) (continue))
          ((< 5 i)  (break)))
    (princ "-- ")))
1 2 -- 3 4 -- 5 6 
--> NIL   

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

THIS IS A 100% MATTER PRODUCT: In the unlikely event that this
merchandise should contact antimatter in any form, a catastrophic
explosion will result.
From: verec
Subject: Re: continuing next iteration
Date: 
Message-ID: <44f9facd$0$639$5a6aecb4@news.aaisp.net.uk>
On 2006-09-02 22:10:14 +0100, Pascal Bourguignon <···@informatimago.com> said:

>  (if (> i 4) (return :interrupted)))
[...]
>            ((< 6 i) (loop-finish)))))

Interesting ...
--
JFB
From: ········@gmail.com
Subject: Re: continuing next iteration
Date: 
Message-ID: <1157248681.141069.161270@m79g2000cwm.googlegroups.com>
Dan Bensen wrote:
> Trying to skip the rest of the body inside a loop.  Is there nothing
> like "continue" in C or "next" in Perl?  All I could find was either go
> to a tag at the end of the body or return from a block or function
> wrapping the body. Are those all the options?

why don't you just wrap the conditional block in WHEN or UNLESS?

(loop .. ... ... stuff stuff (unless test conditional-stuff..)) ?

can you post an example?

> 
> -- 
> My name is dsb, and I'm at prairienet, which is an O-R-G.
From: Dan Bensen
Subject: Re: continuing next iteration
Date: 
Message-ID: <edeo5u$t5q$1@wildfire.prairienet.org>
Dan Bensen wrote:
> All I could find was either
 > go to a tag at the end of the body or
 > return from a block or function wrapping the body.

Tayssir John Gabbour wrote:
 > The DO family (dotimes, etc)  has an implicit tagbody,
 > so I guess you could GO to a label at the end...
 > you could also use BLOCK.

········@gmail.com wrote:
 > not only do the DO/DO*/DOLIST/DOTIMES family wrap ,@body
 > in an implicit block NIL, they wrap it in an implicit TAGBODY
 > so you can use GO within them...

Hmmm... I sense a pattern..

-- 
My name is dsb, and I'm at prairienet, which is an O-R-G.
From: John Thingstad
Subject: Re: continuing next iteration
Date: 
Message-ID: <op.tfbpgewnpqzri1@pandora.upc.no>
On Sun, 03 Sep 2006 16:18:13 +0200, Dan Bensen <··········@cyberspace.net>  
wrote:

> Dan Bensen wrote:
>> All I could find was either
>  > go to a tag at the end of the body or
>  > return from a block or function wrapping the body.
>
> Tayssir John Gabbour wrote:
>  > The DO family (dotimes, etc)  has an implicit tagbody,
>  > so I guess you could GO to a label at the end...
>  > you could also use BLOCK.
>
> ········@gmail.com wrote:
>  > not only do the DO/DO*/DOLIST/DOTIMES family wrap ,@body
>  > in an implicit block NIL, they wrap it in an implicit TAGBODY
>  > so you can use GO within them...
>
> Hmmm... I sense a pattern..
>

The all compile to a tagbody and use goto!
My god what shall we do :)

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/