I frequently run into the problem of having to format a list of values
into a string with some delimiters in between the values. My usual
approach is something like this:
(let ((odds '(1 3 5 7 9)))
(format nil "(~A~{,~A~})" (car odds) (cdr odds)))
in order to get the desired string of:
"(1,3,5,7,9)"
Given the amazing power of 'format', can it really be that there isn't
a more elegant solution?
------------------------+-----------------------------------------------------
Christian Lynbech | christian ··@ defun #\. dk
------------------------+-----------------------------------------------------
Hit the philistines three times over the head with the Elisp reference manual.
- ·······@hal.com (Michael A. Petonic)
On Fri, 28 May 2004, Christian Lynbech wrote:
> I frequently run into the problem of having to format a list of values
> into a string with some delimiters in between the values. My usual
> approach is something like this:
>
> (let ((odds '(1 3 5 7 9)))
> (format nil "(~A~{,~A~})" (car odds) (cdr odds)))
>
> in order to get the desired string of:
>
> "(1,3,5,7,9)"
>
> Given the amazing power of 'format', can it really be that there isn't
> a more elegant solution?
(format nil "(~{~A~^,~})" odds). ~^ means escape if no more elements.
See CLHS for examples of more complicated lists, such as a proper English
list with 'and' or 'or'.
On Fri, 28 May 2004 09:44:49 +0200, Christian Lynbech <·················@ericsson.com> wrote:
> I frequently run into the problem of having to format a list of
> values into a string with some delimiters in between the values. My
> usual approach is something like this:
>
> (let ((odds '(1 3 5 7 9)))
> (format nil "(~A~{,~A~})" (car odds) (cdr odds)))
>
> in order to get the desired string of:
>
> "(1,3,5,7,9)"
>
> Given the amazing power of 'format', can it really be that there
> isn't a more elegant solution?
This is starting to become an FAQ. Third time in less than two
months... :)
Cheers,
Edi.
PS: I see you already got an answer so I won't bother you with that.