From: Andrew Wolven
Subject: C do-while loop
Date: 
Message-ID: <L%Joa.35053$4P1.3123653@newsread2.prod.itd.earthlink.net>
In C there is a looping construct that looks like this:

do
{
some-code
}
while (test);

Some-code is always run once before the test is performed to see if looping
should continue.
I am translating some C code into CL and for the first revision I would like
the code to work as close as possible to the C version.  I cannot find a
loop construct in CL that runs the body code once before it does the test.
I can 'fake-it' by putting a chunk of the same code as the body code in
front of the loop.  In cases where the body code is large, I used a macrolet
(instead of an flet because it contains 'return's), but both these options
still look ugly.
Does anybody know of a more natural way to do this?

Thanks,
AKW

From: Robert STRANDH
Subject: Re: C do-while loop
Date: 
Message-ID: <6wznmkeaxf.fsf@serveur3.labri.fr>
"Andrew Wolven" <·······@nospam.net> writes:

> In C there is a looping construct that looks like this:
> 
> do
> {
> some-code
> }
> while (test);
> 
> Some-code is always run once before the test is performed to see if looping
> should continue.
> I am translating some C code into CL and for the first revision I would like
> the code to work as close as possible to the C version.  I cannot find a
> loop construct in CL that runs the body code once before it does the test.
> I can 'fake-it' by putting a chunk of the same code as the body code in
> front of the loop.  In cases where the body code is large, I used a macrolet
> (instead of an flet because it contains 'return's), but both these options
> still look ugly.
> Does anybody know of a more natural way to do this?

How about:

   (loop do some-code
         while test)

-- 
Robert Strandh
From: Andrew Wolven
Subject: Re: C do-while loop
Date: 
Message-ID: <ZMKoa.35127$4P1.3128983@newsread2.prod.itd.earthlink.net>
"Robert STRANDH" <·······@labri.u-bordeaux.fr> wrote in message
···················@serveur3.labri.fr...
> "Andrew Wolven" <·······@nospam.net> writes:
>
> > In C there is a looping construct that looks like this:
> >
> > do
> > {
> > some-code
> > }
> > while (test);
> >
> > Some-code is always run once before the test is performed to see if
looping
> > should continue.
> > I am translating some C code into CL and for the first revision I would
like
> > the code to work as close as possible to the C version.  I cannot find a
> > loop construct in CL that runs the body code once before it does the
test.
> > I can 'fake-it' by putting a chunk of the same code as the body code in
> > front of the loop.  In cases where the body code is large, I used a
macrolet
> > (instead of an flet because it contains 'return's), but both these
options
> > still look ugly.
> > Does anybody know of a more natural way to do this?
>
> How about:
>
>    (loop do some-code
>          while test)
>
awesome!

> --
> Robert Strandh
>
From: Thomas F. Burdick
Subject: Re: C do-while loop
Date: 
Message-ID: <xcv65p8nysb.fsf@apocalypse.OCF.Berkeley.EDU>
> > "Andrew Wolven" <·······@nospam.net> writes:
> >
> > > In C there is a looping construct that looks like this:
> > >
> > > do
> > > {
> > > some-code
> > > }
> > > while (test);

> "Robert STRANDH" <·······@labri.u-bordeaux.fr> wrote:
>
> > How about:
> >
> >    (loop do some-code
> >          while test)
> >

"Andrew Wolven" <·······@nospam.net> writes:

> awesome!

LOOP is really a wonderful Algol-like language within Lisp.  To cite
myself:
<http://groups.google.com/groups?selm=xcvd75iobl8.fsf%40conquest.OCF.Berkeley.EDU>

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Kenny Tilton
Subject: Re: C do-while loop
Date: 
Message-ID: <3EA38DCB.1050001@nyc.rr.com>
Andrew Wolven wrote:
> "Robert STRANDH" <·······@labri.u-bordeaux.fr> wrote in message
> ···················@serveur3.labri.fr...
> 
>>"Andrew Wolven" <·······@nospam.net> writes:
>>
>>
>>>In C there is a looping construct that looks like this:
>>>
>>>do
>>>{
>>>some-code
>>>}
>>>while (test);
>>>
>>>Some-code is always run once before the test is performed to see if
>>
> looping
> 
>>>should continue.
>>>I am translating some C code into CL and for the first revision I would
>>
> like
> 
>>>the code to work as close as possible to the C version.  I cannot find a
>>>loop construct in CL that runs the body code once before it does the
>>
> test.
> 
>>>I can 'fake-it' by putting a chunk of the same code as the body code in
>>>front of the loop.  In cases where the body code is large, I used a
>>
> macrolet
> 
>>>(instead of an flet because it contains 'return's), but both these
>>
> options
> 
>>>still look ugly.
>>>Does anybody know of a more natural way to do this?
>>
>>How about:
>>
>>   (loop do some-code
>>         while test)
>>
> 
> awesome!

Totally killer. But meta-killer would be:

(defmacro do-while ((&rest do-body) test-form)
   "C do-while"
   (let ((firstp (gensym)))
      `(do ((,firstp t nil))
           ((and (not ,firstp)
                 (not ,test-form))
            (values))
         (progn ,@do-body))))

(let ((times 0))
    (do-while (
       (print "bye bye")
    ) (< (incf times) 3)))

=>

"bye bye"
"bye bye"
"bye bye"


-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"All your bases are belong to us!"
From: Johan Kullstam
Subject: Re: C do-while loop
Date: 
Message-ID: <87smsb91vh.fsf@sysengr.res.ray.com>
Robert STRANDH <·······@labri.u-bordeaux.fr> writes:

> "Andrew Wolven" <·······@nospam.net> writes:
> 
> > In C there is a looping construct that looks like this:
> > 
> > do
> > {
> > some-code
> > }
> > while (test);
> > 
> > Some-code is always run once before the test is performed to see if looping
> > should continue.
> > I am translating some C code into CL and for the first revision I would like
> > the code to work as close as possible to the C version.  I cannot find a
> > loop construct in CL that runs the body code once before it does the test.
> > I can 'fake-it' by putting a chunk of the same code as the body code in
> > front of the loop.  In cases where the body code is large, I used a macrolet
> > (instead of an flet because it contains 'return's), but both these options
> > still look ugly.
> > Does anybody know of a more natural way to do this?
> 
> How about:
> 
>    (loop do some-code
>          while test)

I really like the LOOP code.  However, just to expand the possiblities
and illustrate a use of PROGN, here goes.

(defmacro while (test &body body)
  ;; thanks paul graham onlisp
  `(do ()
       ((not ,test))
     ,@body))


(while (progn some-code
              test))

this is also useful for the more usual (at least for me) C construct

  for(;;)
  {
     some-code;
     if (test) break;
     more-code;
  }

which is

(while (progn some-code
              test)
   more-code)

-- 
Johan KULLSTAM <··········@attbi.com> sysengr
From: Kalle Olavi Niemitalo
Subject: Re: C do-while loop
Date: 
Message-ID: <877k9nsmfr.fsf@Astalo.kon.iki.fi>
Johan Kullstam <··········@attbi.com> writes:

> (while (progn some-code
>               test)
>    more-code)

That is how Emacs Lisp expands the equivalent

  (loop do some-code
        while test
        do more-code)

modulo preparations for return-from and the requirement that DO
must be followed by a compound form.
From: Johan Kullstam
Subject: Re: C do-while loop
Date: 
Message-ID: <87of2z8xg0.fsf@sysengr.res.ray.com>
Kalle Olavi Niemitalo <···@iki.fi> writes:

> Johan Kullstam <··········@attbi.com> writes:
> 
> > (while (progn some-code
> >               test)
> >    more-code)
> 
> That is how Emacs Lisp expands the equivalent
> 
>   (loop do some-code
>         while test
>         do more-code)
> 
> modulo preparations for return-from and the requirement that DO
> must be followed by a compound form.

Btw, now that we are on the topic of emacs, lisp and especially loop,
how do I get emacs to do a decent job of indenting it?  Especially
conditional clauses are pants.  For example

(loop for i from 0 below n
      when (testp i)
         do (some-stuff))

emacs doesn't like indenting the "do" part of the "when".

-- 
Johan KULLSTAM <··········@attbi.com> sysengr