From: Rahul
Subject: List comprehensions in Lisp
Date: 
Message-ID: <1142304613.573337.83950@i40g2000cwc.googlegroups.com>
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.

From: Tim Bradshaw
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <1142333158.659263.169290@j52g2000cwj.googlegroups.com>
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
From: Frank Buss
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <7fheygpqjxqj.e0r0nuxz6xna$.dlg@40tude.net>
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
From: ············@gmail.com
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <1142352404.967949.205450@j52g2000cwj.googlegroups.com>
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
From: Pascal Costanza
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <47ndfcFgf7elU1@individual.net>
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/
From: Rahul
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <1142325890.295984.173790@v46g2000cwv.googlegroups.com>
Whoa !! Thanks !
From: Andru Luvisi
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <87veug6hy5.fsf@localhost.localdomain>
>>>>> "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
From: Edi Weitz
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <u4q2170ii.fsf@agharta.de>
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")
From: ········@gmail.com
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <1142370017.970154.104060@z34g2000cwc.googlegroups.com>
http://www.pedrokroeger.net/weblog/?p=13
From: ········@gmail.com
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <1142370238.698143.118780@z34g2000cwc.googlegroups.com>
>http://www.pedrokroeger.net/weblog/?p=13

lol, I see pedro has posted a response to this thread already

Nick
From: Pedro Kröger
Subject: Re: List comprehensions in Lisp
Date: 
Message-ID: <1142427251.939128.101150@v46g2000cwv.googlegroups.com>
········@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.