format directive "~{...~}"

1 view
Skip to first unread message

David J. Cooper

unread,
Jan 9, 2000, 3:00:00 AM1/9/00
to

Happy New Year to all!

I am wondering about using the ~{....~} format directive,
or a similar format directive, to place something before
all but the first element in an argument list.

First, a belated thanks to everyone who helped clarify the
usage of ~{...~^...~} a few months back.

This has been saving me a lot of messy code lately.

As we now know, the ~^ directive allows one to put something
after each element except the last element in the argument
list, as in:

> (format nil "~{~a~^, ~}" (list 1 2 3 4 5))
"1, 2, 3, 4, 5"

Now I am wondering if there is a way to use this or a similar
directive to put something before all but the first element in
the list, as in:

> (format nil "(~{~a~^,~%~}))" (list one two three))
"(one,
two,
three)"

If possible I would like to extend the above so it outputs

"(one
two
three)"

that is, I want an extra space *before* all but the *first*
element in the argument list, so that the rest of the elements
line up under the first one which has a parenthesis to the left
of it.

In case you are curious, this is for emitting neat-looking SQL
statements, like:

CREATE TABLE TRY
(LAST_NAME VARCHAR(30),
FIRST_NAME VARCHAR(30),
SSN VARCHAR(10))

etc.

Thanks for any wisdom!

-dave

--
David J. Cooper Jr, Chief Engineer Genworks International
dco...@genworks.com 5777 West Maple, Suite 130
(248) 932-2512 (Genworks HQ/voicemail) West Bloomfield, MI 48322-2268
(248) 407-0633 (pager) http://www.genworks.com

David J. Cooper

unread,
Jan 9, 2000, 3:00:00 AM1/9/00
to

David J. Cooper

unread,
Jan 9, 2000, 3:00:00 AM1/9/00
to

David J. Cooper

unread,
Jan 9, 2000, 3:00:00 AM1/9/00
to

David J. Cooper

unread,
Jan 9, 2000, 3:00:00 AM1/9/00
to

Sorry about those duplicated posts! My news server kept saying
it was rejecting the postings when it looks like it wasn't.

Anyways...

"David J. Cooper" wrote:
>
> Now I am wondering if there is a way to use this or a similar
> directive to put something before all but the first element in
> the list, as in:
>

Duh. As I heard from a couple people, and is now obvious, if something
is before all but the first it's after all but the last. So it is
just necessary to put the additional character(s) (in this case a space)
after what I already had:

> (format t "(~{~A~^~% ~})" '(one two three))
(one
two
three)


Thanks to Carl Shapiro and Paul Foley and sorry again about the noise.

Shin

unread,
Jan 9, 2000, 3:00:00 AM1/9/00
to
On Sun, 09 Jan 2000 03:33:40 GMT, "David J. Cooper" <dcoo...@genworks.com>
wrote:

: Now I am wondering if there is a way to use this or a similar
: directive to put something before all but the first element in
: the list, as in:

:
: > (format nil "(~{~a~^,~%~}))" (list one two three))


: "(one,
: two,
: three)"
:
: If possible I would like to extend the above so it outputs
:
: "(one
: two
: three)"

USER(1): (format nil "(~{~a~^,~% ~}))" (list 'one 'two 'three))
"(ONE,
TWO,
THREE))"

Does it help?

-- Shin

Erik Naggum

unread,
Jan 10, 2000, 3:00:00 AM1/10/00
to
* "David J. Cooper" <dcoo...@genworks.com>

| Now I am wondering if there is a way to use this or a similar directive
| to put something before all but the first element in the list, as in:
:
| that is, I want an extra space *before* all but the *first* element in
| the argument list, so that the rest of the elements line up under the
| first one which has a parenthesis to the left of it.

PPRINT-LOGICAL-BLOCK can handle this requirement easily. its FORMAT
interface is documented in section 22.3.5.2 of the standard.

for a list like (one two three) to be formatted nicely, this suffices:

(format nil "~:<~@{~S~^~:@_~}~:>" '(one two three))

untangling this example is left as an exercise for the reader...

#:Erik

Erik Naggum

unread,
Jan 11, 2000, 3:00:00 AM1/11/00
to
* Shin <f...@retemail.es>

| USER(1): (format nil "(~{~a~^,~% ~}))" (list 'one 'two 'three))
| "(ONE,
| TWO,
| THREE))"

for this to work, you would need to do ~& first, and it will fail to
produce the correct output if any forms are nested. using the
pretty-printer interface will work in such cases.

#:Erik

Reply all
Reply to author
Forward
0 new messages