Python has a feature called list comprehensions.
eg : a = [1 2 3 4]
b = [2*x for x in a if x<3]
results in b = [2 4]
Now similar filtering can be achieved by mapcan :see an example in
Cltl2.
I wrote a one line macro which can make this easier :
(defmacro comprehend (symbol lst expr &optional (iftest t))
`(mapcan #'(lambda (,symbol) (and ,iftest (list ,expr))) ,lst))
this can be used as follows :
(comprehend x a (* 2 x) (and (numberp x) (< x 2)))
This is probably more general than the python version since almost
arbitrary expressions can be calculated for both the expression and
test part.
Rahul wrote:
> Python has a feature called list comprehensions.
> eg : a = [1 2 3 4]
> b = [2*x for x in a if x<3]
> results in b = [2 4]
>
> Now similar filtering can be achieved by mapcan :see an example in
> Cltl2.
>
You might also want to look at my GATHER macro, which lets you do stuff
like:
(gather (sqrt x)
for x '(1 2 3 t nil 13)
when (numberp x)
when (> x 0))
it's in http://www.tfeb.org/lisp/toys.html#LIST-COMPREHENSIONS
--tim
Rahul wrote:
> Python has a feature called list comprehensions.
> eg : a = [1 2 3 4]
> b = [2*x for x in a if x<3]
> results in b = [2 4]
>
> Now similar filtering can be achieved by mapcan :see an example in
> Cltl2.
>
> I wrote a one line macro which can make this easier :
>
> (defmacro comprehend (symbol lst expr &optional (iftest t))
> `(mapcan #'(lambda (,symbol) (and ,iftest (list ,expr))) ,lst))
you may find this one useful, too:
http://www.tfeb.org/lisp/toys.html#LIST-COMPREHENSIONS
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Rahul wrote:
> Python has a feature called list comprehensions.
You may want to check the paper "Implementation of a List Comprehension
Macro" by Guy Lapalme:
http://rali.iro.umontreal.ca/Publications/urls/LapalmeLispComp.pdf
Pedro Kroger
Rahul wrote:
> Python has a feature called list comprehensions.
> eg : a = [1 2 3 4]
> b = [2*x for x in a if x<3]
> results in b = [2 4]
>
> Now similar filtering can be achieved by mapcan :see an example in
> Cltl2.
...and the LOOP macro, of course:
(loop for x in a
when (< x 3)
collect (* 2 x))
Pascal
--
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
>>>>> "Rahul" == Rahul <·······@gmail.com> writes:
Rahul> Python has a feature called list comprehensions. eg : a =
Rahul> [1 2 3 4] b = [2*x for x in a if x<3] results in b = [2 4]
At the risk of stating the obvious, this can also be done with mapcar
and remove-if-not, although that does create an intermediate list
while the loop macro approach doesn't.
(setq a '(1 2 3 4))
(setq b (mapcar (lambda (x) (* 2 x))
(remove-if-not (lambda (x) (< x 3)) a)))
Andru
--
Andru Luvisi
Quote Of The Moment:
Knuth's Tex for the Math-kings of sigma, and pi,
Unix vim for the Server-lords with their O'Reilly tomes,
Word for Mortal Men doomed to die,
Emacs from the Bearded One on his Gnu throne,
In the land of Stallman where free software lies.
One Emacs to rule them all. One Emacs to find them,
One Emacs to take commands and to the keystrokes bind them,
In the land of Stallman, where free software lies.
--- Raffael Cavallaro
On 13 Mar 2006 18:50:13 -0800, "Rahul" <·······@gmail.com> wrote:
> Python has a feature called list comprehensions.
> eg : a = [1 2 3 4]
> b = [2*x for x in a if x<3]
> results in b = [2 4]
>
> Now similar filtering can be achieved by mapcan :see an example in
> Cltl2.
>
> I wrote a one line macro which can make this easier :
>
> (defmacro comprehend (symbol lst expr &optional (iftest t))
> `(mapcan #'(lambda (,symbol) (and ,iftest (list ,expr))) ,lst))
>
> this can be used as follows :
>
> (comprehend x a (* 2 x) (and (numberp x) (< x 2)))
>
> This is probably more general than the python version since almost
> arbitrary expressions can be calculated for both the expression and
> test part.
<http://user.it.uu.se/~svenolof/Collect/>
--
European Common Lisp Meeting 2006: <http://weitz.de/eclm2006/>
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
········@gmail.com wrote:
> http://www.pedrokroeger.net/weblog/?p=13
thanks for linking my blog. My original idea was to have lots of
lisp-related posts in English. But I decided I don't like wordpress
very much (although it's a fine piece of software) so I'm not posting
long articles until I change the blog program. I'm looking for some
off-line, emacs-friendly alternative like blogmax.
I just updated this blog post with some nice code Tommy Hallgren has
sent to me.