What is the simplest way of map / filter / reducing a collection
(perhaps a list) in Lisp?
It depends on what you want to do. Have you looked for mapcar, mapcan,
reduce, and many other mapping functions? [1] A few examples:
(reduce #'+ '(1 2 3 4)) => 10
(mapcar #'sqrt '(1 2 3 4)) => (1.0 1.4142135 1.7320508 2.0)
(mapcan #'(lambda (x) (if (oddp x) (list x))) '(1 2 3 4 5))
You should also take look at Practical Common Lisp [2].
Pedro Kroger
[1] http://www.lisp.org/HyperSpec/Body/fun_mapccm_ma_istcm_mapcon.html
[2] http://www.gigamonkeys.com/book/
uhm .. have you checked the hyperspec?
http://www.lispworks.com/documentation/HyperSpec/Front/index.htm
http://www.lispworks.com/documentation/HyperSpec/Body/f_map.htm
http://www.lispworks.com/documentation/HyperSpec/Body/f_reduce.htm
`remove-if-not' is probably what you're after when you mention `filter':
http://www.lispworks.com/documentation/HyperSpec/Body/f_rm_rm.htm
you can download the hyperspec for offline reading here:
http://www.lispworks.com/documentation/common-lisp.html)
--
Lars Rune Nøstdal
http://nostdal.org/
I don't have a reference ready, but note that filtering in Common
Lisp can be done with REMOVE, REMOVE-IF, and REMOVE-IF-NOT; here is a
trivial example (one of many possible):
(reduce #'+ (map 'list #'round (remove-if #'symbolp '(foo 1.2 bar 3.4 baz 5.6))))
=> 10
(see also MAPCAR).
---Vassil.
--
Our programs do not have bugs; it is just that the users' expectations
differ from the way they are implemented.