a question about the (loop ...) mechanism.
How can i do something like the following with loop?
The following is what i would do in SKILL.
(mapcan (lambda ( x)
(mapcar (lambda ( y)
(plus x y))
'( 1 2 3))
'(10 20 30))
--> ( 11 12 13 21 22 23 31 32 33)
I know that in CL plus is replaced by +, but how can i
handle the mapcar inside the mapcan?
-jim
Hi Jim,
Sorry if Google Groups messes up the formatting; it's indented well
when I posted but Google eats the spaces.
Jim Newton wrote:
> a question about the (loop ...) mechanism.
> How can i do something like the following with loop?
>
> The following is what i would do in SKILL.
>
> (mapcan (lambda ( x)
> (mapcar (lambda ( y)
> (plus x y))
> '( 1 2 3))
> '(10 20 30))
>
> --> ( 11 12 13 21 22 23 31 32 33)
>
> I know that in CL plus is replaced by +, but how can i
> handle the mapcar inside the mapcan?
In this case it's a literal translation:
(loop for x in '(10 20 30)
nconc (loop for y in '(1 2 3)
collect (+ x y)))
--> (11 12 13 21 22 23 31 32 33)
MfG,
Tayssir
--
Observations of a circus which travelled to Iraq (note the left
sidebar):
http://www.circus2iraq.org/updates.asp
Jim Newton <·····@rdrop.com> writes:
> a question about the (loop ...) mechanism.
> How can i do something like the following with loop?
>
> The following is what i would do in SKILL.
>
> (mapcan (lambda ( x)
> (mapcar (lambda ( y)
> (plus x y))
> '( 1 2 3))
> '(10 20 30))
>
> --> ( 11 12 13 21 22 23 31 32 33)
>
> I know that in CL plus is replaced by +, but how can i
> handle the mapcar inside the mapcan?
(loop for x from 10 to 30 by 10 nconcing
(loop for y from 1 to 3 collect (+ x y)))
-Peter
--
Peter Seibel ·····@javamonkey.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
Jim Newton <·····@rdrop.com> writes:
> I know that in CL plus is replaced by +, but how can i handle the
> mapcar inside the mapcan?
What do you mean? CL provides the result you requested once 'plus' was
replaced with '+', and the parens were balanced.
--
Frode Vatvedt Fjeld
Frode Vatvedt Fjeld <······@cs.uit.no> writes:
> What do you mean? CL provides the result you requested once 'plus'
> was replaced with '+', and the parens were balanced.
Oh, I didn't notice you asked specifically about loop.. sorry :)
--
Frode Vatvedt Fjeld
(message (Hello 'Jim)
(you :wrote :on '(Fri, 01 Oct 2004 20:23:04 +0200))
(
JN> a question about the (loop ...) mechanism.
JN> How can i do something like the following with loop?
nesting loops are better done with list comprehension, for example, with
COLLECT macro:
CL-USER>
(collect list ((+ x y))
(in x '(1 2 3))
(in y '(10 20 30)))
(11 21 31 12 22 32 13 23 33)
)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
(prin1 "Jane dates only Lisp programmers"))
hi Alex, this is a really great suggestion. however,
i do not find the collect macro in the hyperspec.
-jim
Alex Mizrahi wrote:
> (message (Hello 'Jim)
> (you :wrote :on '(Fri, 01 Oct 2004 20:23:04 +0200))
> (
>
> JN> a question about the (loop ...) mechanism.
> JN> How can i do something like the following with loop?
>
> nesting loops are better done with list comprehension, for example, with
> COLLECT macro:
>
> CL-USER>
> (collect list ((+ x y))
> (in x '(1 2 3))
> (in y '(10 20 30)))
> (11 21 31 12 22 32 13 23 33)
>
> )
> (With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
> (prin1 "Jane dates only Lisp programmers"))
>
>
Jim Newton wrote:
> hi Alex, this is a really great suggestion. however,
> i do not find the collect macro in the hyperspec.
I think he means Sven-Olof Nystrom's list comprehensions, which was
received very well at the Oslo Lisp/Scheme Workshop.
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/slides/Nystrom-slides.pdf
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/submissions/Nystrom.pdf
MfG,
Tayssir
--
Interested in a video class on the US Constitution?
http://www.archive.org/movies/details-db.php?collection=election_2004&collectionid=Michael_Badnarik
>
> -jim
>
> Alex Mizrahi wrote:
> > (message (Hello 'Jim)
> > (you :wrote :on '(Fri, 01 Oct 2004 20:23:04 +0200))
> > (
> >
> > JN> a question about the (loop ...) mechanism.
> > JN> How can i do something like the following with loop?
> >
> > nesting loops are better done with list comprehension, for example,
with
> > COLLECT macro:
> >
> > CL-USER>
> > (collect list ((+ x y))
> > (in x '(1 2 3))
> > (in y '(10 20 30)))
> > (11 21 31 12 22 32 13 23 33)
> >
> > )
> > (With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
> > (prin1 "Jane dates only Lisp programmers"))
> >
> >
On Sat, 02 Oct 2004 06:27:57 -0700, Tayssir John Gabbour wrote:
> Jim Newton wrote:
>> [quoted text muted]
>
> I think he means Sven-Olof Nystrom's list comprehensions, which was
> received very well at the Oslo Lisp/Scheme Workshop.
> http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/slides/Nystrom-slides.pdf
> http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/submissions/Nystrom.pdf
Thanks for that links. Is the code for this extension available anywhere?
Ralf Mattes
> MfG,
> Tayssir
>
> --
> Interested in a video class on the US Constitution?
> http://www.archive.org/movies/details-db.php?collection=election_2004&collectionid=Michael_Badnarik
>
>
>
>> [quoted text muted]
> with
>> [quoted text muted]
R. Mattes wrote:
> On Sat, 02 Oct 2004 06:27:57 -0700, Tayssir John Gabbour wrote:
>
> > Jim Newton wrote:
> >> [quoted text muted]
> >
> > I think he means Sven-Olof Nystrom's list comprehensions, which was
> > received very well at the Oslo Lisp/Scheme Workshop.
> >
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/slides/Nystrom-slides.pdf
> >
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/submissions/Nystrom.pdf
>
> Thanks for that links. Is the code for this extension available
anywhere?
http://user.it.uu.se/~svenolof/Collect/
MfG,
Tayssir
--
Interested in a video class on the US Constitution?
http://www.archive.org/movies/details-db.php?collection=election_2004&collectionid=Michael_Badnarik
>
> Ralf Mattes
>
> > MfG,
> > Tayssir
> >
> > --
> > Interested in a video class on the US Constitution?
> >
http://www.archive.org/movies/details-db.php?collection=election_2004&collectionid=Michael_Badnarik
> >
> >
> >
> >> [quoted text muted]
> > with
> >> [quoted text muted]
Jim Newton <·····@rdrop.com> writes:
> a question about the (loop ...) mechanism.
> How can i do something like the following with loop?
>
> The following is what i would do in SKILL.
>
> (mapcan (lambda ( x)
> (mapcar (lambda ( y)
> (plus x y))
> '( 1 2 3))
> '(10 20 30))
>
> --> ( 11 12 13 21 22 23 31 32 33)
>
> I know that in CL plus is replaced by +, but how can i
> handle the mapcar inside the mapcan?
Someone else has already mentioned loop's nconcing clause, but I
wanted to add my two cents here: I think this style is an example of
one of the pitfalls of using mapping for iteration. mapcan is an
O(n*m) operation -- which doesn't matter when m is small, like in this
example, but using (mapcan ... (mapcar ... as an idiom makes it too
easy to accidentally write inefficient code; especially since
expressing the collection operation explicitly gives you better
efficiency, and better readability.
FWIW, here's how I'd have written the iteration above:
(use-package :org.no-carrier.collectors)
(with-collectors (result)
(dolist (x '(10 20 30))
(dolist (y '(1 2 3))
(collect (+ x y) :into result)))
result)
Or, in unaugmented CL:
(loop with result = ()
for x in '(10 20 30)
do (loop for y in '(1 2 3)
do (push (+ x y) result))
finally (return (nreverse result)))
I'd thought I'd posted the collection utility I use to c.l.l before,
but I can only find an old, less well thought-out version of it. So,
I posted it to small-cl-src here:
http://www.hexapodia.net/pipermail/small-cl-src/2004-October/000041.html