Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Map, Filter, and Reduce in Lisp

713 views
Skip to first unread message

S. Robert James

unread,
Feb 24, 2007, 9:14:48 PM2/24/07
to
I'm familiar with map/filter/reduce from other languages. But I can't
find a simple doc on how to do these in Lisp! What I find perplexing
is that I thought that Lisp invented this paradigm?

What is the simplest way of map / filter / reducing a collection
(perhaps a list) in Lisp?

Pedro Kroger

unread,
Feb 24, 2007, 9:30:34 PM2/24/07
to
On Feb 24, 11:14 pm, "S. Robert James" <srobertja...@gmail.com> wrote:
> 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/

Lars Rune Nøstdal

unread,
Feb 24, 2007, 9:34:22 PM2/24/07
to

Vassil Nikolov

unread,
Feb 24, 2007, 10:51:36 PM2/24/07
to

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.

0 new messages