From: Joakim Hove
Subject: Apply operation to every element in list
Date: 
Message-ID: <lz4s8o8vjg.fsf@metropolis.phys.ntnu.no>
Hello,

I'm a novice in (e)lisp - so please forgive me if this is a silly
question. But - how can I apply an operation to every element in a
list. For instance : 

(setq my-list '("a" "b" ("c" "d") "e" 123))

(defun print-arg (arg)
  (insert "The argument was : " (format "%s" arg) "\n")
)


Now I want to call print-arg (length my-list) times with the different
arguments of my-list. Currently I do this like :

....
(let ((index 0))
   (while (< index (length my-list))
      (print-arg (nth index my-list))      
      (setq index (+ index 1))
   )
)
....

but this feels very inelegant - there must be a better way ??

Any suggestions highly appreciated -- Joakim

-- 
=== Joakim Hove    www.phys.ntnu.no/~hove/     ======================
# Institutt for fysikk  (735) 93637 / E3-166  |  Sk�yensgate 10D    #
# N - 7491 Trondheim    ····@phys.ntnu.no     |  N - 7030 Trondheim #
================================================ 73 93 31 68 ========

From: Tim Bradshaw
Subject: Re: Apply operation to every element in list
Date: 
Message-ID: <ey3bt2w7iag.fsf@cley.com>
* Joakim Hove wrote:
> ....
> (let ((index 0))
>    (while (< index (length my-list))
>       (print-arg (nth index my-list))      
>       (setq index (+ index 1))
>    )
> )
> ....

MAPC:

	(mapc #'print-arg my-list)

will call print-arg on every element of my-list.

Alternatively DOLIST:

	(dolist (e my-list)
	  (print-arg e))

There are many other ways, as well.

These are all CL rather than elisp -- elisp seems to have mapc
natively, and dolist is part of the cl-compatibility package as well.

--tim
From: Michael Kappert
Subject: Re: Apply operation to every element in list
Date: 
Message-ID: <390753BA.A09F7701@iitb.fhg.de>
Tim Bradshaw wrote:

> MAPC:
>
>         (mapc #'print-arg my-list)
>
> will call print-arg on every element of my-list.

> These are all CL rather than elisp -- elisp seems to have mapc
> natively, and dolist is part of the cl-compatibility package as well.

Hmm. My Emacs doesn't know mapc, neither does the elisp manual.
mapc is a built-in function in xemacs.

    (mapcar 'print-arg my-list)

should work in both Emacs and XEmacs, but i'm no expert.
 I think comp.emacs is a good place to ask elisp questions.


Michael

--
From: Otto-Ville Ronkainen
Subject: Re: Apply operation to every element in list
Date: 
Message-ID: <864s8nebmo.fsf@urd.pop.k-net.dk>
Michael Kappert <···@iitb.fhg.de> writes:

> Hmm. My Emacs doesn't know mapc, neither does the elisp manual.

(require 'cl) and see its manual (cl.info).

The way it's implemented in cl.el, mapc doesn't seem to be any more
efficient than mapcar without using its value (though mapc might be
preferable as a matter of style). 
From: Paul Rudin
Subject: Re: Apply operation to every element in list
Date: 
Message-ID: <m3purcrw1p.fsf@shodan.demon.co.uk>
Michael Kappert <···@iitb.fhg.de> writes:

> Tim Bradshaw wrote:
> 
> > MAPC:
> >
> >         (mapc #'print-arg my-list)
> >
> > will call print-arg on every element of my-list.
> 
> > These are all CL rather than elisp -- elisp seems to have mapc
> > natively, and dolist is part of the cl-compatibility package as well.
> 
> Hmm. My Emacs doesn't know mapc, neither does the elisp manual.


try (require 'cl) first. 

> mapc is a built-in function in xemacs.
> 
>     (mapcar 'print-arg my-list)
> 
> should work in both Emacs and XEmacs

yes, but it conses up a list of the results which you (presumably)
don't need.
From: Coby Beck
Subject: Re: Apply operation to every element in list
Date: 
Message-ID: <956780555092@NewsSIEVE.cs.bonn.edu>
Joakim Hove <····@phys.ntnu.no> wrote in message
···················@metropolis.phys.ntnu.no...
| I'm a novice in (e)lisp - so please forgive me if this is a silly
| question. But - how can I apply an operation to every element in a
| list. For instance :
|
| (setq my-list '("a" "b" ("c" "d") "e" 123))
|
| (defun print-arg (arg)
|   (insert "The argument was : " (format "%s" arg) "\n")
| )

(mapcar #'print-arg my-list)

which will do the calls you want and return a list of what each call returned.

Coby
From: Joakim Hove
Subject: Re: Apply operation ... Thanks!
Date: 
Message-ID: <lzvh146ivy.fsf@metropolis.phys.ntnu.no>
Thank's a lot for various suggestions - there had to be a better way
than the one I had implemented

- Joakim


-- 
=== Joakim Hove    www.phys.ntnu.no/~hove/     ======================
# Institutt for fysikk  (735) 93637 / E3-166  |  Sk�yensgate 10D    #
# N - 7491 Trondheim    ····@phys.ntnu.no     |  N - 7030 Trondheim #
================================================ 73 93 31 68 ========