Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

re: ELSE in LOOP

35 views
Skip to first unread message

WJ

unread,
May 23, 2012, 4:12:00 AM5/23/12
to
Michael Kappert wrote:

> 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

Wrong. In SBCL:

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

debugger invoked on a SB-INT:SIMPLE-PROGRAM-ERROR:
A compound form was expected, but NIL found.
current LOOP context: DO NIL ELSE.


Racket:

(for ([i 11])
(if (even? i)
(begin
(displayln i)
(when (= 6 i) (displayln 'Bingo!)))
(displayln 'odd)))

==>
0
odd
2
odd
4
odd
6
Bingo!
odd
8
odd
10

WJ

unread,
May 23, 2012, 4:22:49 AM5/23/12
to
Barry Margolin 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.

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.


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.

WJ

unread,
May 23, 2012, 6:29:48 AM5/23/12
to
WJ wrote:

>
> Racket:
>
> (for ([i 11])
> (if (even? i)
> (begin
> (displayln i)
> (when (= 6 i) (displayln 'Bingo!)))
> (displayln 'odd)))

Shorter:

(for ([i 11])
(displayln (if (even? i) i 'odd))

kodifik

unread,
May 23, 2012, 8:53:30 AM5/23/12
to
The correct form is :
(loop for i from 1 to 10 do
(if (evenp i)
(if (= (print i) 6)
(print 'BINGO!!))
(print 'odd)))

kodifik

unread,
May 23, 2012, 8:56:29 AM5/23/12
to
On 23 mayo, 10:12, "WJ" <w_a_x_...@yahoo.com> wrote:
> Wrong.  In SBCL:
>
> * (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))

Also wrong. Correct :

WJ

unread,
Dec 20, 2014, 2:36:13 AM12/20/14
to
Gauche Scheme:

(do-ec (: i 11)
(if (even? i)
(begin
(print i)
(when (= 6 i) (print 'Bingo!)))
(print 'odd)))

===>

WJ

unread,
Jan 9, 2016, 1:43:43 AM1/9/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
In ABCL:

Debugger invoked on condition of type PROGRAM-ERROR:
A compound form was expected, but NIL found.
Current LOOP context: DO NIL ELSE.


MatzLisp (Ruby):

11.times{|i|
if i.even?
puts i
puts 'Bingo!' if 6==i
else
puts :odd
end}

===>
0
odd
2
odd
4
odd
6
Bingo!
odd
8
odd
10

--
From the New York Times of October 11, 1991, ... we learn that ... researchers
at Boston University admitted that, "There is no question but that Dr. King
plagiarized in the dissertation." ... "Dr. Martin Luther King, Jr." [Michael
King] spent his last night on Earth having sexual intercourse with two women at
the motel and physically beating and abusing a third. -- K. A. Strom
0 new messages