foreach capability
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
You probably already know that (foreach a b (...)) will operate as though a is each of the members of a list b.
And that a is automatically treated as a local.
But what if a and b are the same symbol?
Let's (setq a '(1 2 3 4))
You may think that you have to treat a not a a symbol, but as the value of a symbol...
Command: (foreach a (eval 'a) (print (setq a (* 2 a)))(princ))
2
4
6
8
But you don't. foreach treats the first argument as a local and the second argument as an evaluated list.
So as odd as it may look, you can do the following:
Command: (foreach a a (print (setq a (* 2 a)))(princ))
2
4
6
8
Command: !a
(1 2 3 4)
I don't mean to belittle mapcar, just pointing out a cool feature if you're running out of symbol names or your mapcar gets lost on the back roads.
John F. Uhden