If I want to print a list with the values separated by commas I can
use this handy format recipe:
(format t "~{~a~^,~}" '(1 2 3))
But how I can reproduce this for a Tab Delimited printing? Or, how to
embed a tab character in a lisp string?
Leonardo Varuzza <·······@gmail.com> wrote:
+---------------
| If I want to print a list with the values separated by commas I can
| use this handy format recipe:
|
| (format t "~{~a~^,~}" '(1 2 3))
|
| But how I can reproduce this for a Tab Delimited printing?
| Or, how to embed a tab character in a lisp string?
+---------------
Several ways:
1. Tabs aren't "special" in CL strings; simply insert it:
; A #\Tab is here ---v
> (format t "~&~{~a~^ ~}" '(1 2 3))
1 2 3
NIL
>
2. If you don't need "real" tabs, just tabular spacing,
then you can use the "~T" FORMAT control:
> (format t "~&~{~a~^~0,8t~}" '(1 2 3))
1 2 3
NIL
>
3. If you need real #\Tab characters but don't want to have
obscure bugs because of unexpected whitespace reformatting
[e.g., some editors "helpfully" silently replace spaces
by tabs or vice-versa under certain random conditions],
then you can do something like this:
> (let ((fmt (format nil "~~&~~{~~a~~^~c~~}" #\Tab)))
(format t fmt '(1 2 3)))
1 2 3
NIL
>
-Rob
p.s. I have tried to have the only tabs in this reply be
only the five instances that *should* be there in the example
codes & outputs above, but my editor and/or the netnews system
may tweak the result between me & thee. If so, then my most
abject apologies in advance...
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
On Oct 7, 10:34 am, ····@rpw3.org (Rob Warnock) wrote:
> 3. If you need real #\Tab characters but don't want to have
> obscure bugs because of unexpected whitespace reformatting
> [e.g., some editors "helpfully" silently replace spaces
> by tabs or vice-versa under certain random conditions],
> then you can do something like this:
>
> > (let ((fmt (format nil "~~&~~{~~a~~^~c~~}" #\Tab)))
> (format t fmt '(1 2 3)))
> 1 2 3
> NIL
> >
Even though I sometimes put tab literals in format strings, I do think
it's pretty poor style. Even if your editor leaves it alone, the tab
might display as, say, a single space due to the column it started
in. And while constructing format strings with format can be
powerful, this is a case where it's too much headache for what you
get: the above code is waaaaaay to hard to read for its modest task.
Instead, I think this is a great time to use concatenate:
(let ((format #.(concatenate 'string "~&~{~a~^" '(#\Tab) "~}")))
(format t format '(1 2 3)))
On Sun, 07 Oct 2007 03:26:43 +0000, Leonardo Varuzza wrote:
> If I want to print a list with the values separated by commas I can
> use this handy format recipe:
>
> (format t "~{~a~^,~}" '(1 2 3))
>
> But how I can reproduce this for a Tab Delimited printing? Or, how to
> embed a tab character in a lisp string?
Well, this works:
(format t "~{~a~^ ~}" '(1 2 3))
There is a tab character after the caret.
The following is a bit verbose, I guess, but it works:
(format t (concatenate 'string
"~{~a~^"
(make-string 1 :initial-element #\Tab)
"~}")
'(1 2 3))
I'll bet there are other fun and educational ways to do it! :-)
Cheers,
David Trudgett