I find myself wanting to execute something in a loop *except for* the
last iteration. Is there some way to concisely express this? Even a
last iteration type thing. The "then" subclause of an "as/for" is
useful for detecting the first time.
Example, I want to print a list of numbers seperated by commas except
at the last one which should end with a period and newline.
(loop for num in list-of-numbers
initially (format t "~&")
do (format t "~D" num)
finally (format t ".~%"))
would do it except for the lack of commas (or seperating spaces which
would be nice too). How do i smoothly figure out if it is the last
time through?
Something like
(loop for num in list-of-numbers
initially (format t "~&")
do (format t "~D" num)
not-finally (format t ", ")
finally (format t ".~%"))
--
Johan KULLSTAM <··········@attbi.com> sysengr
Johan Kullstam <··········@attbi.com> writes:
> I find myself wanting to execute something in a loop *except for* the
> last iteration. Is there some way to concisely express this? Even a
> last iteration type thing. The "then" subclause of an "as/for" is
> useful for detecting the first time.
>
> Something like
>
> (loop for num in list-of-numbers
> initially (format t "~&")
> do (format t "~D" num)
> not-finally (format t ", ")
> finally (format t ".~%"))
Well, there's
(loop for nums on list-of-numbers
initially (format t "~&")
do (format t "~D" (car nums))
if (cdr nums)
do (format t ", ")
finally (format t ".~%"))
which probably doesn't qualify as smooth. For smooth, how about
(format t "~&~{~D~^, ~}.~%" list-of-numbers)
?
Cheers,
Christophe
--
Jesus College, Cambridge, CB5 8BL +44 1223 510 299
http://www-jcsu.jesus.cam.ac.uk/~csr21/ (defun pling-dollar
(str schar arg) (first (last +))) (make-dispatch-macro-character #\! t)
(set-dispatch-macro-character #\! #\$ #'pling-dollar)
Christophe Rhodes <·····@cam.ac.uk> writes:
> (loop for nums on list-of-numbers
> initially (format t "~&")
> do (format t "~D" (car nums))
> if (cdr nums)
> do (format t ", ")
> finally (format t ".~%"))
This trick makes it perhaps slightly smoother:
(loop for (number . more-numbers-p) on list-of-numbers
...)
--
Frode Vatvedt Fjeld
Frode Vatvedt Fjeld <······@acm.org> writes:
> Christophe Rhodes <·····@cam.ac.uk> writes:
>
> > (loop for nums on list-of-numbers
> > initially (format t "~&")
> > do (format t "~D" (car nums))
> > if (cdr nums)
> > do (format t ", ")
> > finally (format t ".~%"))
>
> This trick makes it perhaps slightly smoother:
>
> (loop for (number . more-numbers-p) on list-of-numbers
> ...)
that is a nice trick. thank you to all who have responded.
--
Johan KULLSTAM <··········@attbi.com> sysengr
Johan Kullstam <··········@attbi.com> writes:
>
> Frode Vatvedt Fjeld <······@acm.org> writes:
>
> > Christophe Rhodes <·····@cam.ac.uk> writes:
> >
> > > (loop for nums on list-of-numbers
> > > initially (format t "~&")
> > > do (format t "~D" (car nums))
> > > if (cdr nums)
> > > do (format t ", ")
> > > finally (format t ".~%"))
> >
> > This trick makes it perhaps slightly smoother:
> >
> > (loop for (number . more-numbers-p) on list-of-numbers
> > ...)
>
> that is a nice trick. thank you to all who have responded.
>
> --
> Johan KULLSTAM <··········@attbi.com> sysengr
It is also often possible to transform most processing which
depends on output between items to handle the first item
differently rather than the last. For example, using the
above code:
(when list-of-numbers
(loop for num in (rest list-of-numbers)
initially (format t "~&~D" (first numbers))
do (format t ", ~D"num)
finally (format t ".~%")))
Determining the first element is easy, whereas it can be
rather trickier to do it with the last element.
--
Thomas A. Russ, USC/Information Sciences Institute ···@isi.edu
Johan Kullstam wrote:
> I find myself wanting to execute something in a loop *except for* the
> last iteration. Is there some way to concisely express this?
When you're looping through a list, you can use
(loop for (thing . rest) on list
when rest
do (operate-on thing) ; this will not happen for the last thing in list
...)
--
Arthur Lemmens
In article <··············@sysengr.res.ray.com>, Johan Kullstam <··········@attbi.com> wrote:
> I find myself wanting to execute something in a loop *except for* the
> last iteration. Is there some way to concisely express this? Even a
> last iteration type thing. The "then" subclause of an "as/for" is
> useful for detecting the first time.
I don't know about this one. but
>
> Example, I want to print a list of numbers seperated by commas except
> at the last one which should end with a period and newline.
I'd use FORMAT for this particular case:
(format t "~&~{~#[~;~D.~:;~D,~]~}~%" '(1 2 3 4))
refs:
22.3 Formatted Output
22.3.7.2 Tilde Left-Bracket: Conditional Expression
22.3.7.4 Tilde Left-Brace: Iteration
--
This message was not sent to you unsolicited.
····@ma.ccom (Takehiko Abe) writes:
>
> In article <··············@sysengr.res.ray.com>, Johan Kullstam <··········@attbi.com> wrote:
>
>
> >
> > Example, I want to print a list of numbers seperated by commas except
> > at the last one which should end with a period and newline.
>
> I'd use FORMAT for this particular case:
>
> (format t "~&~{~#[~;~D.~:;~D,~]~}~%" '(1 2 3 4))
There is an even simpler format form:
(format t "~&~{~D~^, ~}.~%" '(1 2 3 4))
The ~^ causes the list iteration to halt at that point if there
are no more arguments coming.
> refs:
> 22.3 Formatted Output
> 22.3.7.2 Tilde Left-Bracket: Conditional Expression
> 22.3.7.4 Tilde Left-Brace: Iteration
22.3.9.2 Tilde Circumflex: Escape Upward
--
Thomas A. Russ, USC/Information Sciences Institute ···@isi.edu
Johan Kullstam <··········@attbi.com> wrote:
>
> Example, I want to print a list of numbers separated by commas except
> at the last one which should end with a period and newline.
····@ma.ccom (Takehiko Abe) writes:
>
> I'd use FORMAT for this particular case:
>
> (format t "~&~{~#[~;~D.~:;~D,~]~}~%" '(1 2 3 4))
···@sevak.isi.edu (Thomas A. Russ) writes:
>
> There is an even simpler format form:
>
> (format t "~&~{~D~^, ~}.~%" '(1 2 3 4))
... that doesn't meet the requirements.
(format t "~&·@[~{~D~^, ~}.~]~%" '(1 2 3 4))
is possible.
--
"I believe in the horse. The automobile is a passing phenomenon."
-- Kaiser Wilhelm II. (1859-1941)