help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: print out all members of a list


From: Pascal J. Bourguignon
Subject: Re: print out all members of a list
Date: Tue, 01 Mar 2011 06:41:01 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

ken <address@hidden> writes:

> On 02/28/2011 10:20 AM ken wrote:
>> (car '("one" "two" "three"))
>> 
>> prints out "one" ... the first of the list.  How to print out all
>> elements of the list (in order and with the double quotes around them?
>> I'm actually looking just to substitute something for "car" and not
>> write an entire function.  Or is there no such thing?
>> 
>> Thanks much.
>> 
>
> I've been criticized for my elisp terminology-- and properly so--, so
> let me rephrase:
>
> (car '("one" "two" "three"))
>
> returns a string consisting of the first element (?) of the list.  Is
> there an elisp function which either (1) returns one string for each
> element of the list or

Not in emacs lisp, since emacs lisp function can return only ONE result.
They cannot return several results.

Even in Common Lisp which can return several results, there is an
implementation defined on the maximum number of results a function can
return (which must be at least 20, but can also be at most 20 even if
it's often bigger) so you cannot in general return one result per
element of a list, since lists are not limited in length apart from the
memory available.


> (2) returns one string containing all elements of
> the list?

What's wrong with the list itself?  

It already contains all the elements of the list,
and if they're strings as your list seems to contain, it already
contains all the elements of the list (itself) as strings.

In anycase, if you don't mind the parentheses, you can get the list
represented as a string with:

    (let ((list '("one" "two" "tree")))
      (prin1-to-string list))

    --> "(\"one\" \"two\" \"tree\")"


You can also trivially remove the parentheses:

    (let ((list '("one" "two" "tree")))
      (substring (prin1-to-string list) 1 -1))

    --> "\"one\" \"two\" \"tree\""


Now, if the elements of the list are not strings, and you want them to
be strings, you can do so with:

    (let ((list '(one 2 [t h r e e])))
      (mapcar (function prin1-to-string) list))

    --> ("one" "2" "[t h r e e]")
   

and if you still insist to have the result as a single string:

    (let ((list '(one 2 [t h r e e])))
      (substring (prin1-to-string (mapcar (function prin1-to-string) list)) 1 
-1))

    --> "\"one\" \"2\" \"[t h r e e]\""

But I repeat, there's little point in converting lists into strings,
since it's so much easier to process data when it's structured into
lists, than when it's mere characters in a string.


> P.S. It seems strange that elisp has so many ways to manipulate lists,
> but doesn't seem to have this very simple functionality.

Yes.  Think about it!


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]