From: Johan Kullstam
Subject: LOOP and "anti-finally"?
Date: 
Message-ID: <m3k7mu3iip.fsf@sysengr.res.ray.com>
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

From: Christophe Rhodes
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <sqit2essig.fsf@lambda.jcn.srcf.net>
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)
From: Frode Vatvedt Fjeld
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <2hznvqhg27.fsf@vserver.cs.uit.no>
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
From: Johan Kullstam
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <m3wuqu4p44.fsf@sysengr.res.ray.com>
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
From: Thomas A. Russ
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <ymir8gylidr.fsf@sevak.isi.edu>
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    
From: Arthur Lemmens
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <3D5952F9.42BE4AA6@xs4all.nl>
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
From: Takehiko Abe
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <keke-1408020045270001@solg4.keke.org>
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.
From: Thomas A. Russ
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <ymisn1elikd.fsf@sevak.isi.edu>
····@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    
From: Wolfhard Buß
Subject: Re: LOOP and "anti-finally"?
Date: 
Message-ID: <m3znvml22z.fsf@buss-14250.user.cis.dfn.de>
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)