From: S. Robert James
Subject: Map, Filter, and Reduce in Lisp
Date: 
Message-ID: <1172369688.487577.18110@8g2000cwh.googlegroups.com>
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?

From: Pedro Kroger
Subject: Re: Map, Filter, and Reduce in Lisp
Date: 
Message-ID: <1172370634.413060.233470@h3g2000cwc.googlegroups.com>
On Feb 24, 11:14 pm, "S. Robert James" <············@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/
From: Vassil Nikolov
Subject: Re: Map, Filter, and Reduce in Lisp
Date: 
Message-ID: <yy8vwt26nbcn.fsf@eskimo.com>
On 24 Feb 2007 18:14:48 -0800, "S. Robert James" <············@gmail.com> said:

| 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?

  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.
From: Lars Rune Nøstdal
Subject: Re: Map, Filter, and Reduce in Lisp
Date: 
Message-ID: <pan.2007.02.25.02.34.21.890109@gmail.com>
On Sat, 24 Feb 2007 18:14:48 -0800, S. Robert James wrote:

> 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?


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/