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
"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
"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
>
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!"
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
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.
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