From: Don Geddis
Subject: FORMAT idiom/puzzle
Date: 
Message-ID: <87pt8wfkpi.fsf@sidious.geddis.org>
A lot of times I have a list
      (defvar *things* '(apple banana carrot))
and I want to print it out with some in-between marker (e.g. comma-separated).
This is almost right:
      (format t "~{, ~A~}" *things*)
      => , APPLE, BANANA, CARROT
but unfortunately there's the extra leading comma.  So I wind up doing this
instead:
      (when *things*
        (format t "~A~{, ~A~}" (first *things*) (rest *things*)) )

This seems awkward and annoying to me, both the WHEN part (to avoid printing
anything if *THINGS* is NIL), and also the FIRST/REST part.  It seems like
the FORMAT language is probably rich enough to do this itself, but with a
quick glance at the relevant section I've been unable to construct the
appropriate magic.

Anybody know a clever FORMAT string which has the effect of the code above,
but be written more like this:
      (format t *clever-fmt-str* *things*)
What value of *CLEVER-FMT-STR* does the right thing?

Thanks,

        -- Don
_______________________________________________________________________________
Don Geddis                  http://don.geddis.org/               ···@geddis.org
Save a tree.  Eat a beaver.

From: Thomas F. Burdick
Subject: Join strings using FORMAT (was: FORMAT idiom/puzzle)
Date: 
Message-ID: <xcvad00ul8w.fsf@famine.OCF.Berkeley.EDU>
Don Geddis <···@geddis.org> writes:

> A lot of times I have a list
>       (defvar *things* '(apple banana carrot))
>
> and I want to print it out with some in-between marker (e.g. comma-separated).
> This is almost right:
>       (format t "~{, ~A~}" *things*)
>       => , APPLE, BANANA, CARROT
> but unfortunately there's the extra leading comma.

Try putting the subject line into Google groups ;-)

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Joe Marshall
Subject: Re: FORMAT idiom/puzzle
Date: 
Message-ID: <ad001cqs.fsf@comcast.net>
Don Geddis <···@geddis.org> writes:

> A lot of times I have a list
>       (defvar *things* '(apple banana carrot))
> and I want to print it out with some in-between marker (e.g. comma-separated).
> This is almost right:
>       (format t "~{, ~A~}" *things*)
>       => , APPLE, BANANA, CARROT
> but unfortunately there's the extra leading comma.  So I wind up doing this
> instead:
>       (when *things*
>         (format t "~A~{, ~A~}" (first *things*) (rest *things*)) )
>
> This seems awkward and annoying to me, both the WHEN part (to avoid printing
> anything if *THINGS* is NIL), and also the FIRST/REST part.  It seems like
> the FORMAT language is probably rich enough to do this itself, but with a
> quick glance at the relevant section I've been unable to construct the
> appropriate magic.
>
> Anybody know a clever FORMAT string which has the effect of the code above,
> but be written more like this:
>       (format t *clever-fmt-str* *things*)
> What value of *CLEVER-FMT-STR* does the right thing?

····@{~S~^, ~}~}"

-- 
~jrm
From: Lawrence Mitchell
Subject: Re: FORMAT idiom/puzzle
Date: 
Message-ID: <?fnord?87brkgqnn6.fsf@ID-97657.usr.dfncis.de>
Don Geddis wrote:

> A lot of times I have a list
>       (defvar *things* '(apple banana carrot))
> and I want to print it out with some in-between marker (e.g. comma-separated).
> This is almost right:
>       (format t "~{, ~A~}" *things*)
>       => , APPLE, BANANA, CARROT
> but unfortunately there's the extra leading comma.  So I wind up doing this
> instead:
>       (when *things*
>         (format t "~A~{, ~A~}" (first *things*) (rest *things*)) )

I think you want the ~^ directive (22.3.9.2):

(let ((things '(apple banana carrot)))
  (format t "~{~A~^, ~}" things))

=> APPLE, BANANA, CARROT
NIL

(let ((things nil))
  (format t "~{~A~^, ~}" things))

=>
NIL

[...]

-- 
Lawrence Mitchell <·····@gmx.li>
From: Edi Weitz
Subject: Re: FORMAT idiom/puzzle
Date: 
Message-ID: <874qq8i87f.fsf@agharta.de>
On Sat, 22 May 2004 19:06:59 GMT, Don Geddis <···@geddis.org> wrote:

> A lot of times I have a list
>       (defvar *things* '(apple banana carrot))
> and I want to print it out with some in-between marker
> (e.g. comma-separated).
> This is almost right:
>       (format t "~{, ~A~}" *things*)
>       => , APPLE, BANANA, CARROT
> but unfortunately there's the extra leading comma.  So I wind up
> doing this instead:
>       (when *things*
>         (format t "~A~{, ~A~}" (first *things*) (rest *things*)) )
>
> This seems awkward and annoying to me, both the WHEN part (to avoid
> printing anything if *THINGS* is NIL), and also the FIRST/REST part.
> It seems like the FORMAT language is probably rich enough to do this
> itself, but with a quick glance at the relevant section I've been
> unable to construct the appropriate magic.
>
> Anybody know a clever FORMAT string which has the effect of the code
> above, but be written more like this:
>       (format t *clever-fmt-str* *things*)
> What value of *CLEVER-FMT-STR* does the right thing?

  * (format nil "~{~A~^, ~}" '(apple banana carrot))

  "APPLE, BANANA, CARROT"
  * (format nil "~{~A~^, ~}" '(apple))

  "APPLE"
  * (format nil "~{~A~^, ~}" '())

  ""

See also the 'join' thread:

  <http://groups.google.com/groups?threadm=3A8AC19A.DDA1ECA5%40eurocom.od.ua>

Edi.