3

How do I perform a function on every element in a list? For example say I have a list:

(1 x q)

and I was to use my-function on 1, x, q. Is there a predefined function for this? Similarly to foreach in a "higher" level language? Or do I have to manually step through it using car and cdr?

Any help would be much appreciated!

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Arthur Putnam
  • 1,026
  • 1
  • 9
  • 26
  • 8
    Stackoverflow is not the tool for learning the basics of a programming language. It is expected that you put yourself some effort into solving a problem. For learning the basics of Lisp see this book with a PDF version for download: Common Lisp: A Gentle Introduction to Symbolic Computation, http://www.cs.cmu.edu/~dst/LispBook/ Especially see chapter 7. – Rainer Joswig Sep 07 '14 at 20:25
  • 2
    See http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list-processing.html#mapping, http://www.gigamonkeys.com/book/macros-standard-control-constructs.html#looping and http://www.gigamonkeys.com/book/loop-for-black-belts.html. – uselpa Sep 07 '14 at 20:47
  • 1
    I looked for loops to when trying to learn Lisp. Use [MAPCAR](http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm) or loop eg. `(loop :for element :in '(1 x q) :collect (list element))`. I recommend buying [Land of Lisp](http://www.landoflisp.com/) – Sylwester Sep 07 '14 at 21:09
  • `...in a "higher" level language?` - hey, I wouldn't say that Lisp is "lower" level programming language compared with any of them. – Mark Karpov Sep 08 '14 at 09:05
  • 2
    "higher level"? Lisp exists on all levels, if the programmer wants; it can soar on levels of which lesser languages don't even dare to dream. – molbdnilo Sep 09 '14 at 10:52
  • if you heard about "manually step[ping] through it using car and cdr", why don't you write some code along those lines? don't be afraid of reinventing the wheel, that's how you learn the language. After you become more comfortable with the language, you will start using library functions more and more. – Will Ness Sep 10 '14 at 23:23
  • 2
    Given the plethora of options, this is a valid question for several levels of lisp knowledge or skill. Try assuming that the asker IS being very specific: they don't need the return value. That's how my google-fu led me to this specific question. I'm thinking that MAP is what I need, as opposed to MAPCAR, but I'm not sure yet... – daveloyall Dec 24 '15 at 01:53
  • With respect, hard disagree on Mr. Joswig's description of stackoverflow's purpose (though in agreement with his book recommendation). It is not a particularly valuable use of one's time to memorize the entire suite of language quirks when one really only needs to be reminded how one language expresses the concept of "for-each"---a concept so common, in fact, I was surprised the answer wasn't actually "Common Lisp has a built-in 'for-each'. But no; LISP is so ancient it has its own outlier names for the common concept, and finding those outlier names is exactly what stackoverflow is for. – fixermark Sep 14 '18 at 02:10

2 Answers2

5

If you want to construct a fresh list of results of my-function, use mapcar:

(mapcar #'my-function my-list)

If you do not want the results, use mapc or dolist or loop.

See also

  1. What's the best way to learn LISP?
  2. The #' in common lisp
  3. Why #' is used before lambda in Common Lisp?
sds
  • 58,617
  • 29
  • 161
  • 278
4

Of course there is.

Look a the hyperspec symbol index and read about the following:

  • map, map-into
  • mapcar, mapcan, mapcon, mapc, mapl, maplist
  • dolist
  • loop: (loop :for element :in list #|...|#)

If you need the results as another list of the same length: map, map-into, mapcar, loop. If you want to do it by side effects: dolist, loop.

Svante
  • 50,694
  • 11
  • 78
  • 122