unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* print out all members of a list
@ 2011-02-28 15:20 ken
  2011-02-28 16:50 ` Teemu Likonen
                   ` (5 more replies)
  0 siblings, 6 replies; 14+ messages in thread
From: ken @ 2011-02-28 15:20 UTC (permalink / raw)
  To: GNU Emacs List

(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.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
       [not found] <mailman.4.1298906433.17550.help-gnu-emacs@gnu.org>
@ 2011-02-28 15:24 ` Marc Mientki
  0 siblings, 0 replies; 14+ messages in thread
From: Marc Mientki @ 2011-02-28 15:24 UTC (permalink / raw)
  To: help-gnu-emacs

Am 28.02.2011 16:20, schrieb ken:
> (car '("one" "two" "three"))

(mapcar 'print '("one" "two" "three"))

regards
Marc




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-02-28 15:20 ken
@ 2011-02-28 16:50 ` Teemu Likonen
  2011-02-28 17:21 ` ken
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 14+ messages in thread
From: Teemu Likonen @ 2011-02-28 16:50 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

* 2011-02-28 10:20 (-0500), 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?

CAR doesn't print anything, it _returns_ a value. I'm not sure what you
mean, maybe this:

    (identity '("one" "two" "three"))

IDENTITY just returns its argument. If you need some kind of
pretty-printing you could try PP function.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-02-28 15:20 ken
  2011-02-28 16:50 ` Teemu Likonen
@ 2011-02-28 17:21 ` ken
  2011-02-28 18:32   ` Thierry Volpiatto
  2011-02-28 20:26   ` PJ Weisberg
       [not found] ` <mailman.0.1298913700.1451.help-gnu-emacs@gnu.org>
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 14+ messages in thread
From: ken @ 2011-02-28 17:21 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

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 (2) returns one string containing all elements of
the list?

E.g.:

(1) "one" "two" "three"

or

(2) "onetwothree"

preferably (1).


Thanks again.


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






^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-02-28 17:21 ` ken
@ 2011-02-28 18:32   ` Thierry Volpiatto
  2011-02-28 20:26   ` PJ Weisberg
  1 sibling, 0 replies; 14+ messages in thread
From: Thierry Volpiatto @ 2011-02-28 18:32 UTC (permalink / raw)
  To: help-gnu-emacs

ken <gebser@mousecar.com> 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 (2) returns one string containing all elements of
> the list?
>
> E.g.:
>
> (1) "one" "two" "three"
(loop for i in '("one" "two" "three") do (princ (concat "\"" i "\"" " ")))

> or
>
> (2) "onetwothree"
(mapconcat 'identity '("one" "two" "three") " ")
==>"one two three"

> preferably (1).
>
>
> Thanks again.
>
>
> P.S. It seems strange that elisp has so many ways to manipulate lists,
> but doesn't seem to have this very simple functionality.
Because "one" "two" "three" is unusable in code if not in a container.
(i.e it is three differents objects, but not one)

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-02-28 17:21 ` ken
  2011-02-28 18:32   ` Thierry Volpiatto
@ 2011-02-28 20:26   ` PJ Weisberg
  1 sibling, 0 replies; 14+ messages in thread
From: PJ Weisberg @ 2011-02-28 20:26 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

On 2/28/11, ken <gebser@mousecar.com> wrote:
> 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 (2) returns one string containing all elements of
> the list?
>
> E.g.:
>
> (1) "one" "two" "three"
>
> or
>
> (2) "onetwothree"
>
> preferably (1).

I'm not sure what (1) would actually *mean*, since a list of three
strings is what you already have, but if you did want to print each of
them out, then perhaps something like

(mapc 'insert '("one" "two" "three"))

-PJ



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
       [not found] ` <mailman.0.1298913700.1451.help-gnu-emacs@gnu.org>
@ 2011-03-01  5:41   ` Pascal J. Bourguignon
  0 siblings, 0 replies; 14+ messages in thread
From: Pascal J. Bourguignon @ 2011-03-01  5:41 UTC (permalink / raw)
  To: help-gnu-emacs

ken <gebser@mousecar.com> 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 {}.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-02-28 15:20 ken
                   ` (2 preceding siblings ...)
       [not found] ` <mailman.0.1298913700.1451.help-gnu-emacs@gnu.org>
@ 2011-03-01 11:33 ` Andreas Röhler
  2011-03-01 11:44 ` Andreas Röhler
       [not found] ` <mailman.5.1298979610.15512.help-gnu-emacs@gnu.org>
  5 siblings, 0 replies; 14+ messages in thread
From: Andreas Röhler @ 2011-03-01 11:33 UTC (permalink / raw)
  To: help-gnu-emacs

Am 28.02.2011 16:20, schrieb ken:
> (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.
>
>


Hi,

I like `dolist'

eval this


(let ((my-list (list "one" "two" "three")))
     (dolist (elem my-list)
   (insert (concat "\nHere I am!: " elem))))


HTH


Andreas

--
https://code.launchpad.net/~a-roehler/python-mode/python-mode-components
https://code.launchpad.net/s-x-emacs-werkstatt/




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-02-28 15:20 ken
                   ` (3 preceding siblings ...)
  2011-03-01 11:33 ` Andreas Röhler
@ 2011-03-01 11:44 ` Andreas Röhler
       [not found] ` <mailman.5.1298979610.15512.help-gnu-emacs@gnu.org>
  5 siblings, 0 replies; 14+ messages in thread
From: Andreas Röhler @ 2011-03-01 11:44 UTC (permalink / raw)
  To: help-gnu-emacs

Am 28.02.2011 16:20, schrieb ken:
> (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.
>
>

and still a form delivering with doublequotes...

(let ((my-list (list "one" "two" "three")))
     (dolist (elem my-list)
   (insert (format "\n\"%s\"" elem))))




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
       [not found] ` <mailman.5.1298979610.15512.help-gnu-emacs@gnu.org>
@ 2011-03-01 13:10   ` Pascal J. Bourguignon
  2011-03-01 14:45     ` Stefan Monnier
                       ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Pascal J. Bourguignon @ 2011-03-01 13:10 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> Am 28.02.2011 16:20, schrieb ken:
>> (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.
>>
>>
>
> and still a form delivering with doublequotes...
>
> (let ((my-list (list "one" "two" "three")))
>     (dolist (elem my-list)
>   (insert (format "\n\"%s\"" elem))))

Why do you write broken code?

    (let ((my-list (list "o\"n\"e" "t\"wo" "th\\\"ree")))
       (dolist (elem my-list)
          (insert (format "\n\"%s\"" elem))))

    "o"n"e"
    "t"wo"
    "th\"ree"


Is it not easier to write code that works?

    (let ((my-list (list "o\"n\"e" "t\"wo" "th\\\"ree")))
       (dolist (elem my-list)
          (insert (format "\n%s" (prin1-to-string elem)))))

    "o\"n\"e"
    "t\"wo"
    "th\\\"ree"


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-03-01 13:10   ` Pascal J. Bourguignon
@ 2011-03-01 14:45     ` Stefan Monnier
  2011-03-01 14:50     ` Le Wang
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2011-03-01 14:45 UTC (permalink / raw)
  To: help-gnu-emacs

>           (insert (format "\n%s" (prin1-to-string elem)))))

You meant

        (insert (format "\n%S elem))))

right?


        Stefan


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-03-01 13:10   ` Pascal J. Bourguignon
  2011-03-01 14:45     ` Stefan Monnier
@ 2011-03-01 14:50     ` Le Wang
  2011-03-01 15:10     ` Richard Riley
  2011-03-01 18:39     ` Andreas Röhler
  3 siblings, 0 replies; 14+ messages in thread
From: Le Wang @ 2011-03-01 14:50 UTC (permalink / raw)
  To: Pascal J. Bourguignon; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 311 bytes --]

prin1-to-string knows how to quote strings properly ... got it.

On Tue, Mar 1, 2011 at 9:10 PM, Pascal J. Bourguignon <pjb@informatimago.com
> wrote:

> Why do you write broken code?
>

...


> Is it not easier to write code that works?


But maybe we don't have to talk like cthun to make our points?

-- 
Le

[-- Attachment #2: Type: text/html, Size: 805 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-03-01 13:10   ` Pascal J. Bourguignon
  2011-03-01 14:45     ` Stefan Monnier
  2011-03-01 14:50     ` Le Wang
@ 2011-03-01 15:10     ` Richard Riley
  2011-03-01 18:39     ` Andreas Röhler
  3 siblings, 0 replies; 14+ messages in thread
From: Richard Riley @ 2011-03-01 15:10 UTC (permalink / raw)
  To: help-gnu-emacs

"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

> Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
>
>> Am 28.02.2011 16:20, schrieb ken:
>>> (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.
>>>
>>>
>>
>> and still a form delivering with doublequotes...
>>
>> (let ((my-list (list "one" "two" "three")))
>>     (dolist (elem my-list)
>>   (insert (format "\n\"%s\"" elem))))
>
> Why do you write broken code?

I'm intrigued. Do you get a bonus point for every time you are rude and
condescending to people looking for help or are patiently helping
others?


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: print out all members of a list
  2011-03-01 13:10   ` Pascal J. Bourguignon
                       ` (2 preceding siblings ...)
  2011-03-01 15:10     ` Richard Riley
@ 2011-03-01 18:39     ` Andreas Röhler
  3 siblings, 0 replies; 14+ messages in thread
From: Andreas Röhler @ 2011-03-01 18:39 UTC (permalink / raw)
  To: help-gnu-emacs

Am 01.03.2011 14:10, schrieb Pascal J. Bourguignon:
> Andreas Röhler<andreas.roehler@easy-emacs.de>  writes:
>
>> Am 28.02.2011 16:20, schrieb ken:
>>> (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.
>>>
>>>
>>
>> and still a form delivering with doublequotes...
>>
>> (let ((my-list (list "one" "two" "three")))
>>      (dolist (elem my-list)
>>    (insert (format "\n\"%s\"" elem))))
>
> Why do you write broken code?
>
>      (let ((my-list (list "o\"n\"e" "t\"wo" "th\\\"ree")))
>         (dolist (elem my-list)
>            (insert (format "\n\"%s\"" elem))))
>
>      "o"n"e"
>      "t"wo"
>      "th\"ree"
>
>
> Is it not easier to write code that works?
>
>      (let ((my-list (list "o\"n\"e" "t\"wo" "th\\\"ree")))
>         (dolist (elem my-list)
>            (insert (format "\n%s" (prin1-to-string elem)))))
>
>      "o\"n\"e"
>      "t\"wo"
>      "th\\\"ree"
>
>

Ok, thanks, interesting point.

 From there `concat' read better for me

(let ((my-list (list "o\"n\"e" "t\"wo" "th\\\"ree")))
        (dolist (elem my-list)
           (insert (concat "\n" (prin1-to-string elem)))))

(?) :-)






^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2011-03-01 18:39 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.4.1298906433.17550.help-gnu-emacs@gnu.org>
2011-02-28 15:24 ` print out all members of a list Marc Mientki
2011-02-28 15:20 ken
2011-02-28 16:50 ` Teemu Likonen
2011-02-28 17:21 ` ken
2011-02-28 18:32   ` Thierry Volpiatto
2011-02-28 20:26   ` PJ Weisberg
     [not found] ` <mailman.0.1298913700.1451.help-gnu-emacs@gnu.org>
2011-03-01  5:41   ` Pascal J. Bourguignon
2011-03-01 11:33 ` Andreas Röhler
2011-03-01 11:44 ` Andreas Röhler
     [not found] ` <mailman.5.1298979610.15512.help-gnu-emacs@gnu.org>
2011-03-01 13:10   ` Pascal J. Bourguignon
2011-03-01 14:45     ` Stefan Monnier
2011-03-01 14:50     ` Le Wang
2011-03-01 15:10     ` Richard Riley
2011-03-01 18:39     ` Andreas Röhler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).