Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
ELSE in LOOP
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  16 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Sungwoo Lim  
View profile  
 More options Apr 5 2001, 10:42 am
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk (Sungwoo Lim)
Date: Thu, 05 Apr 2001 15:41:15 +0100
Local: Thurs, Apr 5 2001 10:41 am
Subject: [Q] ELSE in LOOP
Hello,

I confused to use ELSE in LOOP.
Anyone explain me why following code is wrong?
Also, is it right to use RETURN NIL when the ELSE doesn't
has any clause?

;----------------
(loop for i from 0 to 10
      when (evenp i)
           do (print i)
           when (= i 6)
                do (print 'BINGO!!)
           else
                return nil
      else
           do (print 'odd))

> Error: Secondary clause misplaced at top level in LOOP macro: ELSE DO
(PRINT 'ODD) ...
>        Current LOOP context: ELSE DO.

Thanks for your help.

Sungwoo

--
<^)++<


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Apr 5 2001, 10:54 am
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Thu, 05 Apr 2001 14:53:39 GMT
Local: Thurs, Apr 5 2001 10:53 am
Subject: Re: [Q] ELSE in LOOP
In article <sungwoo-0504011541150...@gorecki.cad.strath.ac.uk>,

I think indenting it correctly might clarify the problem:

(loop for i from 0 to 10
      when (evenp i)
           do (print i)
      when (= i 6)
           do (print 'BINGO!!)
      else
           return nil
  else
       do (print 'odd))

The second WHEN ends the first WHEN, so the second ELSE is dangling.

The body of a DO clause is just regular Lisp expressions, and it ends when
the first symbol is encountered.  You can't have a WHEN inside a DO; if you
want conditionals inside the DO clause, you have to use a regular (if ...)
invocation:

(loop for i from 0 to 10
      when (evenp i)
           do (print i)
              (if (= i 6)
                  (print 'BINGO!!)
                  (return nil))
      else
           do (print 'odd))

I like LOOP, but I have a general rule of thumb about it: the more complex
a LOOP gets, the less appropriate it is to use.  It's great for some simple
constructs that it automates, such as collecting results, but if you try to
do too many things with it at once you're likely to run into problems, and
you should go back to "normal" Lisp code.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sungwoo Lim  
View profile  
 More options Apr 5 2001, 11:32 am
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk (Sungwoo Lim)
Date: Thu, 05 Apr 2001 16:30:53 +0100
Local: Thurs, Apr 5 2001 11:30 am
Subject: Re: [Q] ELSE in LOOP
In article <TF%y6.291$U4.10880@burlma1-snr2>, Barry Margolin

I see...

My codes mixed two hierarchical LOOP and four hierarchical WHEN, so I was
struggling to solve it. I should change it as a easy way then...
Thanks alot. =)

Sungwoo

--
<^)++<


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Kappert  
View profile  
 More options Apr 5 2001, 11:50 am
Newsgroups: comp.lang.lisp
From: Michael Kappert <k...@iitb.fhg.de>
Date: Thu, 05 Apr 2001 17:42:44 +0200
Local: Thurs, Apr 5 2001 11:42 am
Subject: Re: [Q] ELSE in LOOP
Sungwoo Lim schrieb:

You can't use DO to group statements containing LOOP keywords,
because a LOOP keyword implicitly terminates the DO body.
Use AND to join the statements in a WHEN branch.

RETURN terminates the loop. If you didn't mean to terminate on the
first even number, use a dummy else branch.

As a matter of style, i suggest using IF instead of WHEN
if (err, when :-)) the ELSE branch is present.

So, your code would appear as

(loop for i from 0 to 10
    if (evenp i)
      do (print i)
      and if (= i 6)
            do (print 'BINGO!!)
          else do ()
    else
      do (print 'odd))

which gives

0
odd
2
odd
4
odd
6
bingo!!
odd
8
odd
10
odd
nil

However, I'd advise you to use the conditional keywords only in very
simple cases. When things get more complicated, use normal Lisp forms
instead:

(loop for i from 0 to 10 do
      (cond ((evenp i)
             (print i)
             (when (= i 6) (print 'bingo!!)))
            (T
             (print 'odd))))

Michael

--


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frode Vatvedt Fjeld  
View profile  
 More options Apr 5 2001, 11:50 am
Newsgroups: comp.lang.lisp
From: Frode Vatvedt Fjeld <fro...@acm.org>
Date: 05 Apr 2001 17:47:44 +0200
Local: Thurs, Apr 5 2001 11:47 am
Subject: Re: [Q] ELSE in LOOP
[I first replied privately in email by mistake. Sorry about that.]

sung...@cad.strath.ac.uk (Sungwoo Lim) writes:
> I confused to use ELSE in LOOP.  Anyone explain me why following
> code is wrong?

I think you are confused not about the use of ELSE, but of the purpose
of LOOP's conditional constructs.

Their purpose is primarily to control the execution of LOOP's value
accumulation clauses. At least it helps to think of them this
way. That is, LOOP's conditional execution clauses are _not_ to be
used as general control structures, substituting the regular lisp
control structures.

So my rule of thumb would be: Use LOOP's conditional execution clauses
only where you can't use the regular lisp control operators.

(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.)

> ;----------------
> (loop for i from 0 to 10
>       when (evenp i)
>            do (print i)
>            when (= i 6)
>                 do (print 'BINGO!!)
>            else
>                 return nil
>       else
>            do (print 'odd))

This is not a good example because it's not very realistic. And you
can't nest clauses like that. If you have a more realistic example,
it'd be easier to suggest improvements.

In general however, you need to factor out the loop termination
condition(s), you don't want them embedded deep in other
code. Applying this to your contrieved example you'd get something
like this (it still doesn't make sense):

  (loop for i from 0 to 10
     as terminate = (and (evenp i) (not (= i 6)))
     do (cond
          ((evenp i)
           (print i)
           (when (= i 6) (print 'bingo)))
          (t (print 'odd)))
     until terminate)

If you _really_ need to terminate your loop from deep within other
functional code, consider using RETURN-FROM or throwing a
condition. I'd expect this to be _very_ infrequently required, though.

--
Frode Vatvedt Fjeld


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sungwoo Lim  
View profile  
 More options Apr 5 2001, 12:18 pm
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk (Sungwoo Lim)
Date: Thu, 05 Apr 2001 17:17:27 +0100
Local: Thurs, Apr 5 2001 12:17 pm
Subject: Re: [Q] ELSE in LOOP
In article <2hsnjn2vrz....@dslab7.cs.uit.no>, Frode Vatvedt Fjeld

<fro...@acm.org> wrote:
> This is not a good example because it's not very realistic. And you
> can't nest clauses like that. If you have a more realistic example,
> it'd be easier to suggest improvements.

OK, here is the part of my code. Still confused,
so I am trying to change the WHEN and ELSE in LOOP to IF clauses (or may
be COND is better?).
Thanks for your help.

Sungwoo

;-----------
(loop for i from 0 to stroke-index
      when (= (aref stroke-array-y i) (dialine-y1 (aref stroke-array-x i)))
          when (and (> (aref stroke-array-x i) max-inter-x)
                    (> (aref stroke-array-y i) max-inter-y))
              do (setf max-inter-x (aref stroke-array-x i)
                       max-inter-y (aref stroke-array-y i))
          else
              when (and (< (aref stroke-array-x i) min-inter-x)
                        (< (aref stroke-array-y i) min-inter-y))
                  do (setf min-inter-x (aref stroke-array-x i)
                           min-inter-y (aref stroke-array-y i))
              else
                  return nil
      else
          when (/= deno 0)
              do (setf Ua (/ numer1 deno) Ub (/ numer2 deno))
              when (and (<= 0 Ua 1) (<= 0 Ub 1) (/= x1 x3) (/= y1 y3))
                  do (setf temp-x (float (+ x1 (* Ua (- x2 x1))))
                           temp-y (float (+ y1 (* Ua (- y2 y1)))))
                  when (and (> temp-x max-inter-x) (> temp-y max-inter-y))
                      do (setf max-inter-x temp-x max-inter-y temp-y)
                  else
                      when (and (< temp-x min-inter-x) (< temp-y min-inter-y))
                          do (setf min-inter-x temp-x min-inter-y temp-y))

--
<^)++<


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Kappert  
View profile  
 More options Apr 5 2001, 12:40 pm
Newsgroups: comp.lang.lisp
From: Michael Kappert <k...@iitb.fhg.de>
Date: Thu, 05 Apr 2001 18:33:52 +0200
Local: Thurs, Apr 5 2001 12:33 pm
Subject: Re: [Q] ELSE in LOOP
Sungwoo Lim schrieb:

                    ^^^^^^
This terminates the loop alltogether. Do you want that?
The first part of the loop seems to search some kind of convex
hull, so i think you must loop over all points (if the sequence
of points is monotonic, this makes no sense).
You might want to look into scanline algorithms & such.

>       else
>           when (/= deno 0)
>               do (setf Ua (/ numer1 deno) Ub (/ numer2 deno))
>               when (and (<= 0 Ua 1) (<= 0 Ub 1) (/= x1 x3) (/= y1 y3))
>                   do (setf temp-x (float (+ x1 (* Ua (- x2 x1))))
>                            temp-y (float (+ y1 (* Ua (- y2 y1)))))
>                   when (and (> temp-x max-inter-x) (> temp-y max-inter-y))
>                       do (setf max-inter-x temp-x max-inter-y temp-y)
>                   else
>                       when (and (< temp-x min-inter-x) (< temp-y min-inter-y))
>                           do (setf min-inter-x temp-x min-inter-y temp-y))

> --
> <^)++<

--

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sungwoo Lim  
View profile  
 More options Apr 5 2001, 12:48 pm
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk (Sungwoo Lim)
Date: Thu, 05 Apr 2001 17:47:20 +0100
Local: Thurs, Apr 5 2001 12:47 pm
Subject: Re: [Q] ELSE in LOOP
In article <3ACC9E70.451CD...@iitb.fhg.de>, Michael Kappert

<k...@iitb.fhg.de> wrote:
> This terminates the loop alltogether. Do you want that?

No, I don't want it. Yeap, that was what I didn't know.
The case of ELSE, I want to by pass,
so what should I fill in that ELSE clause for doing nothing?

> The first part of the loop seems to search some kind of convex
> hull, so i think you must loop over all points (if the sequence
> of points is monotonic, this makes no sense).
> You might want to look into scanline algorithms & such.

Hmm, where can I find the scanline algorithms, etc?
Any good pointer for that please?
Thanks alot.

Sungwoo

--
<^)++<


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Apr 5 2001, 2:24 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Thu, 05 Apr 2001 18:23:15 GMT
Local: Thurs, Apr 5 2001 2:23 pm
Subject: Re: [Q] ELSE in LOOP
In article <2hsnjn2vrz....@dslab7.cs.uit.no>,
Frode Vatvedt Fjeld  <fro...@acm.org> wrote:

>(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.  Most other
parts of Lisp have relatively straightforward semantics, they nest in
obvious ways, and interactions are pretty clear (perhaps the only notable
exception is the interaction between all the different exiting mechanisms
like RETURN, UNWIND-PROTECT, and condition handlers -- it was very
difficult to get that right in the standard).

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Apr 5 2001, 2:26 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Thu, 05 Apr 2001 18:25:28 GMT
Local: Thurs, Apr 5 2001 2:25 pm
Subject: Re: [Q] ELSE in LOOP
In article <sungwoo-0504011747200...@gorecki.cad.strath.ac.uk>,

Sungwoo Lim <sung...@cad.strath.ac.uk> wrote:
>In article <3ACC9E70.451CD...@iitb.fhg.de>, Michael Kappert
><k...@iitb.fhg.de> wrote:

>> This terminates the loop alltogether. Do you want that?

>No, I don't want it. Yeap, that was what I didn't know.
>The case of ELSE, I want to by pass,
>so what should I fill in that ELSE clause for doing nothing?

  else
      do (progn)

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Kappert  
View profile  
 More options Apr 5 2001, 3:20 pm
Newsgroups: comp.lang.lisp
From: Michael Kappert <k...@iitb.fhg.de>
Date: Thu, 05 Apr 2001 21:11:25 +0200
Local: Thurs, Apr 5 2001 3:11 pm
Subject: Re: [Q] ELSE in LOOP

Sungwoo Lim wrote:
> > The first part of the loop seems to search some kind of convex
> > hull, so i think you must loop over all points (if the sequence
> > of points is monotonic, this makes no sense).
> > You might want to look into scanline algorithms & such.

> Hmm, where can I find the scanline algorithms, etc?

Scanline algorithms are used in computer graphics,
for computing inner regions of polytopes, or computing
2D views of 3D scenes, for example.

> Any good pointer for that please?

Sorry, I don't have any particular references.
Try Google (www.google.com) with the search keywords
scanline algorithm or geometric algorithm.

Michael

--


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kent M Pitman  
View profile  
 More options Apr 5 2001, 3:34 pm
Newsgroups: comp.lang.lisp
From: Kent M Pitman <pit...@world.std.com>
Date: Thu, 5 Apr 2001 19:31:33 GMT
Local: Thurs, Apr 5 2001 3:31 pm
Subject: Re: [Q] ELSE in LOOP

Barry Margolin <bar...@genuity.net> writes:
> In article <sungwoo-0504011747200...@gorecki.cad.strath.ac.uk>,
> Sungwoo Lim <sung...@cad.strath.ac.uk> wrote:
> >In article <3ACC9E70.451CD...@iitb.fhg.de>, Michael Kappert
> ><k...@iitb.fhg.de> wrote:

> >> This terminates the loop alltogether. Do you want that?

> >No, I don't want it. Yeap, that was what I didn't know.
> >The case of ELSE, I want to by pass,
> >so what should I fill in that ELSE clause for doing nothing?

>   else
>       do (progn)

Or, if you're wanting to be more abstract

(defmacro nothing () 'nil)

Then later:
  ...
  else
    do (nothing)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Apr 5 2001, 3:57 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: 05 Apr 2001 19:53:45 +0000
Local: Thurs, Apr 5 2001 3:53 pm
Subject: Re: [Q] ELSE in LOOP
* Sungwoo Lim

> The case of ELSE, I want to by pass,
> so what should I fill in that ELSE clause for doing nothing?

  Use end to terminate a conditional clause.

  if ... do ... end

  From 6.1.6 Conditional Execution Clauses

The optional loop keyword end marks the end of the clause.  If this keyword
is not supplied, the next loop keyword marks the end.  The construct end
can be used to distinguish the scoping of compound clauses.

#:Erik
--
  I found no peace in solitude.
  I found no chaos in catastrophe.
                        -- :wumpscut:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christopher J. Vogt  
View profile  
 More options Apr 5 2001, 4:26 pm
Newsgroups: comp.lang.lisp
From: "Christopher J. Vogt" <v...@computer.org>
Date: Thu, 05 Apr 2001 20:25:53 GMT
Local: Thurs, Apr 5 2001 4:25 pm
Subject: Re: [Q] ELSE in LOOP

Sungwoo Lim wrote:

> Hmm, where can I find the scanline algorithms, etc?
> Any good pointer for that please?
> Thanks alot.

You can most likely find what you are looking for in one of the Graphics Gems books, probably the first one.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Eh? where are last posts?" by Sungwoo Lim
Sungwoo Lim  
View profile  
 More options Apr 6 2001, 4:00 am
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk (Sungwoo Lim)
Date: Fri, 06 Apr 2001 09:00:26 +0100
Local: Fri, Apr 6 2001 4:00 am
Subject: Eh? where are last posts?
Where are they?
There were couple of answers more for my question but thety disappeared?
Strange..

Sungwoo

--
<^)++<


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "ELSE in LOOP" by Lieven Marchand
Lieven Marchand  
View profile  
 More options Apr 6 2001, 11:52 am
Newsgroups: comp.lang.lisp
From: Lieven Marchand <m...@wyrd.be>
Date: 05 Apr 2001 22:21:56 +0200
Local: Thurs, Apr 5 2001 4:21 pm
Subject: Re: [Q] ELSE in LOOP

Barry Margolin <bar...@genuity.net> writes:
> 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.

In a way it is a pity that the MIT LOOP user defined iteration paths
didn't make the standard, since the ADD-LOOP-PATH arguments give a
conceptual model of loop clauses, splitting the effects of each clause
into bindings, prologue, pre-step, step, post-step and pseudo-step.

--
Lieven Marchand <m...@wyrd.be>
Glaðr ok reifr skyli gumna hverr, unz sinn bíðr bana.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »