From: mike on
Hi

Formatting a list as a comma-delimited string is easy;

(format nil "~{~a~^,~}" '(1 2 3)) => "1,2,3"

How do i do the same (SBCL) but with tab characters in the output?
It must be simple but I've tried a ton of different things.

Cheers

Mike.
From: Vassil Nikolov on

On Sun, 1 Nov 2009 23:00:46 -0800 (PST), mike <mpheasant(a)gmail.com> said:
> Formatting a list as a comma-[separated] string is easy;

> (format nil "~{~a~^,~}" '(1 2 3)) => "1,2,3"

> How do i do the same (SBCL) but with tab characters in the output?

The simplest is to have a literal #\Tab in the format string:

(format nil "~{~a~^ ~}" '(1 2 3))
^ this is a #\Tab (``C-q TAB'' with emacs)

The next two simplest are (i) to construct the format string along
the lines of

(format nil (format nil "~~{~~a~~^~C~~}" #\Tab) '(1 2 3))

and (ii) to call a user-defined format function along the lines of

(format nil "~{~a~^~:*~/cl-user::tab/~}" '(1 2 3))

where

(defun cl-user::tab (stream &rest ignored)
(declare (ignore ignored))
(write-char #\Tab stream))

(the possibly redundant package marker above is there to illustrate
how the function name could be in any package).

Apart from the semi-portability of #\Tab, all of the above are
portable.

---Vassil.


--
"Even when the muse is posting on Usenet, Alexander Sergeevich?"
From: Madhu on

* mike <4dec6822-0069-450a-9561-5b3aa8da1a55(a)i12g2000prg.googlegroups.com> :
Wrote on Sun, 1 Nov 2009 23:00:46 -0800 (PST):

| Hi
|
| Formatting a list as a comma-delimited string is easy;
|
| (format nil "~{~a~^,~}" '(1 2 3)) => "1,2,3"
|
| How do i do the same (SBCL) but with tab characters in the output?
| It must be simple but I've tried a ton of different things.

Just embed a TAB character which has char-code 9 in the format string
instead of the comma char-code 44. If your editor is set to convert all
tabs to spaces, you lose. Then you can use the `22.3.5.4 Tilde Slash:
Call Function' facility of FORMAT like this:

(defun cl-user::printtab (stream &rest args)
(declare (ignore args))
(write-char #\Tab stream))

(format nil "~{~a~^~:*~/printtab/~}" '(1 2 3))

This technique is useful when you want to delimit lists with arbitrary
strings specified at runtime: I'll point you to the following CLL post
by Juho snellman:

,----
| From: Juho Snellman <jsnell(a)iki.fi>
| Subject: Re: Newby riddle
| Date: 22 Aug 2007 17:54:53 GMT
| Message-ID: <slrnfcou3d.57r.jsnell(a)sbz-30.cs.Helsinki.FI>
| (57r.jsnell@)
`----

Or to the `Re: Emacs Lisp's "mapconcat" in Common Lisp?'
thread from july this year

,----
| Subject: Re: Emacs Lisp's "mapconcat" in Common Lisp?
| Date: Mon, 13 Jul 2009 21:20:01 +0530
| Message-ID: <m3k52cy446.fsf(a)moon.robolove.meer.net>
| (m3k52cy446.fsf @ moon)
`----

--
Madhu

From: Kyle M on
On Nov 2, 2:00 am, mike <mpheas...(a)gmail.com> wrote:
> Hi
>
> Formatting a list as a comma-delimited string is easy;
>
> (format nil "~{~a~^,~}" '(1 2 3))  =>  "1,2,3"
>
> How do i do the same (SBCL) but with tab characters in the output?
> It must be simple but I've tried a ton of different things.
>
> Cheers
>
> Mike.

Personally, when this comes up, I just avoid the iteration in format
altogether.

(reduce (lambda (x y) (format nil "~A~C~A" x #\tab y)) '(1 2 3))

OTOH if you want to use just one format string, the above posts using
~/function/ seem to be the best way to do it.
From: Vassil Nikolov on

On Mon, 2 Nov 2009 04:14:16 -0800 (PST), Kyle M <kylemcg(a)gmail.com> said:
> Personally, when this comes up, I just avoid the iteration in format
> altogether.

> (reduce (lambda (x y) (format nil "~A~C~A" x #\tab y)) '(1 2 3))

How would you do the above so that it works equally well for stream
output destinations, not just when a string is produced (never mind
that it is slower in the latter case), i.e. when the first argument
to FORMAT is a parameter, and also for any format directive such as
`~S' for the elements?

In other words, neither

(reduce (lambda (x y) (format t "~A~C~A" x #\tab y)) '(1 2 3))

nor

(reduce (lambda (x y) (format nil "~S~C~S" x #\tab y)) '(1 2 3))

produce a desired result.

---Vassil.


--
"Even when the muse is posting on Usenet, Alexander Sergeevich?"
 |  Next  |  Last
Pages: 1 2 3
Prev: faster?
Next: Article about lisp and array languages