From: basman
Subject: variables within loop in format
Date: 
Message-ID: <1181153605.898006.142550@a26g2000pre.googlegroups.com>
I have a list L, and a variable *field-width*.  I want to print the
elements of L in columns with width *field-width*.  Is there a better
way to do this than
(format t "~:{~0,·@T~a~}" (mapcar #'(lambda (x) (list *field-width*
x))))
?
- Bhaskara

From: basman
Subject: Re: variables within loop in format
Date: 
Message-ID: <1181153746.929134.182470@o11g2000prd.googlegroups.com>
On Jun 6, 2:13 pm, basman <········@gmail.com> wrote:
> I have a list L, and a variable *field-width*.  I want to print the
> elements of L in columns with width *field-width*.  Is there a better
> way to do this than
> (format t "~:{~0,·@T~a~}" (mapcar #'(lambda (x) (list *field-width*
> x))))
> ?
> - Bhaskara

That should actually be
(format t "~:{~0,·@T~a~}" (mapcar #'(lambda (x) (list *field-width*
x)) l))
From: Thomas A. Russ
Subject: Re: variables within loop in format
Date: 
Message-ID: <ymizm3csyh0.fsf@sevak.isi.edu>
basman <········@gmail.com> writes:

> On Jun 6, 2:13 pm, basman <········@gmail.com> wrote:
> > I have a list L, and a variable *field-width*.  I want to print the
> > elements of L in columns with width *field-width*.  Is there a better
> > way to do this than
> 
> That should actually be
> (format t "~:{~0,·@T~a~}" (mapcar #'(lambda (x) (list *field-width*
> x)) l))

Well, I can think of two simplifications.  I don't think that you need
to use the ~T to get the column widths.  It would be simpler to just
pass the *field-width* parameter to ~A itself:

  (format t "~:{~v,A~}" 
          (mapcar #'(lambda (x) (list *field-width* x)) l))

But as I'm sure you suspect, the need to package the values into pairs
is less than elegant.  If I were to do this, I would either use nested
format statements to generate a custom format string, or I would use a
simpler format statement inside of a loop.

  (format t (format nil "~~:{~~~D,A~~}" *field-width*) l)

  (dolist (i l)
    (format t "~v,A" *field-width* i))

  (mapcar #'(lambda (x) (format t "~v,A" *field-width* i)) l)

Of these, I prefer the middle one, since the intent is most clear.  I
usually reserve the first method for situations where I need to make use
of more complicated formatting options in the format string.

Note: If you want the alignment to be right-aligned, then you can
specify the ·@" flag to ~A.

-- 
Thomas A. Russ,  USC/Information Sciences Institute