From: globalrev
Subject: builtin for generating list?
Date: 
Message-ID: <80bb4cf2-15dc-4845-affc-63819e0d72d3@c58g2000hsc.googlegroups.com>
if i want a list with the numbers between x and y is there a buil in
function to generate this?

in python i can say range(x,y,step) so i can do:
map(lambda x:x*x*x,range(1,101,2)) which creates a list with all the
cubes of the odd numbers between 1 and 100.

From: Willem Broekema
Subject: Re: builtin for generating list?
Date: 
Message-ID: <c68cbf1e-5b4e-45f8-9b26-7a222d2ff20c@f36g2000hsa.googlegroups.com>
On May 13, 11:26 pm, globalrev <·········@yahoo.se> wrote:
> if i want a list with the numbers between x and y is there a buil in
> function to generate this?

Yep, use LOOP.

> in python i can say range(x,y,step) so i can do:
> map(lambda x:x*x*x,range(1,101,2)) which creates a list with all the
> cubes of the odd numbers between 1 and 100.

(loop for x from 1 to 101 by 2 collect (* x x x))
=> (1 27 125 343 729 1331 2197 3375 4913 6859 ...)

Note that loop also allows counting down, but in that case you should
use "downfrom" with a positive step, instead of "from" with a negative
step:

(loop for x downfrom 101 to 1 by 2 collect (* x x x))
=> (1030301 970299 912673 857375 804357 753571 704969 658503 614125
571787 ...)

It shouldn't be too long before "range" is working in CLPython btw,
but right now it gives:

clpython(32): (repl)
[CLPython -- type `:q' to quit, `:help' for help]
>>> range(1,101,2)
Error: todo: range with 3 args


- Willem
From: ···············@gmail.com
Subject: Re: builtin for generating list?
Date: 
Message-ID: <4f530183-e939-4d50-91eb-b8d62fa534aa@j22g2000hsf.googlegroups.com>
On May 13, 10:26 pm, globalrev <·········@yahoo.se> wrote:
> if i want a list with the numbers between x and y is there a buil in
> function to generate this?
>
> in python i can say range(x,y,step) so i can do:
> map(lambda x:x*x*x,range(1,101,2)) which creates a list with all the
> cubes of the odd numbers between 1 and 100.

Untested but something like:

(defun range (start end &optional (step 1))
  (loop for n from start below end by step collect n))

(mapcar (lambda (x) (* x x x)) (range 1 101 2))

--
Phil
http://phil.nullable.eu/
From: Rob Warnock
Subject: Re: builtin for generating list?
Date: 
Message-ID: <_6WdnaHLPoBB97fVnZ2dnUVZ_sHinZ2d@speakeasy.net>
<···············@gmail.com> wrote:
+---------------
| globalrev <·········@yahoo.se> wrote:
| > if i want a list with the numbers between x and y is there a buil in
| > function to generate this?
| >
| > in python i can say range(x,y,step) so i can do:
| > map(lambda x:x*x*x,range(1,101,2)) which creates a list with all the
| > cubes of the odd numbers between 1 and 100.
| 
| Untested but something like:
| 
| (defun range (start end &optional (step 1))
|   (loop for n from start below end by step collect n))
| (mapcar (lambda (x) (* x x x)) (range 1 101 2))
+---------------

Here's mine, which is *very* similar but with a slightly different
signature which better matches the way I tend to use it:

    (defun iota (count &optional (start 0) (step 1))
      (loop repeat count for i upfrom start by step collect i))

    (mapcar (lambda (x) (* x x x)) (iota 100 1 2))


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: GP lisper
Subject: Re: builtin for generating list?
Date: 
Message-ID: <slrng2o4mi.ifl.spambait@phoenix.clouddancer.com>
On Tue, 13 May 2008 23:30:20 -0500, <····@rpw3.org> wrote:
><···············@gmail.com> wrote:

>| (defun range (start end &optional (step 1))
>|   (loop for n from start below end by step collect n))
>| (mapcar (lambda (x) (* x x x)) (range 1 101 2))
> +---------------
>
> Here's mine, which is *very* similar but with a slightly different
> signature which better matches the way I tend to use it:
>
>     (defun iota (count &optional (start 0) (step 1))
>       (loop repeat count for i upfrom start by step collect i))
>
>     (mapcar (lambda (x) (* x x x)) (iota 100 1 2))

Since there are only 50 steps between 1 and 101, I think I like the
start-end-step version a bit more.

-- 
One of the strokes of genius from McCarthy
was making lists the center of the language - kt
** Posted from http://www.teranews.com **
From: Rob Warnock
Subject: Re: builtin for generating list?
Date: 
Message-ID: <-r2dncPr3b0s67DVnZ2dnUVZ_oWdnZ2d@speakeasy.net>
GP lisper  <········@clouddancer.com> wrote:
+---------------
| <····@rpw3.org> wrote:
| ><···············@gmail.com> wrote:
| >| (defun range (start end &optional (step 1))
| >|   (loop for n from start below end by step collect n))
| >| (mapcar (lambda (x) (* x x x)) (range 1 101 2))
| > +---------------
| >
| > Here's mine, which is *very* similar but with a slightly different
| > signature which better matches the way I tend to use it:
| >     (defun iota (count &optional (start 0) (step 1))
| >       (loop repeat count for i upfrom start by step collect i))
| >     (mapcar (lambda (x) (* x x x)) (iota 100 1 2))
| 
| Since there are only 50 steps between 1 and 101, I think I like the
| start-end-step version a bit more.
+---------------

Ah, yes, That should have been (IOTA 50 1 2), of course.
Thanks for the catch.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Robert Maas, http://tinyurl.com/uh3t
Subject: Re: builtin for generating list?
Date: 
Message-ID: <rem-2008may13-003@yahoo.com>
> From: globalrev <·········@yahoo.se>
> if i want a list with the numbers between x and y

That's not possible, because there are potentially an infinite
number of (rational) numbers between any two given (rational)
numbers. On the other hand, the set of *INTEGERS* between two
(rational) numbers would be finite.
Is that what you really meant to ask?

> is there a buil [sic] in function to generate this?

For *INTEGERS*, this is a trivial algorithm for you to write
yourself using LOOP or DO or PROG or DOTIMES, so why do you care if
there's a built-in function to do all your work with just one
function call? It's not like you're going to waste six hours of
work re-inventing something already in CL. So you waste half a
minute re-inventing the function yourself. That's faster than
composing a question to a newsgroup and waiting for a reply.

> in python i can say range(x,y,step) so i can do:
> map(lambda x:x*x*x,range(1,101,2)) which creates a list with all
> the cubes of the odd numbers between 1 and 100.

That's a stupid way to do it, building an explicit list of indices
which will be used just to map to cubes, when you could do the
whole thing in one step by directly looping over the range (without
building a list of indices) to generate and collect the cubes.
Of course if you already know how to generate the list of indices,
and you know how to map over the indices, then it makes sense to
combine the two steps rather than worry about doing the one-step
method you don't know about. You know the joke about the
mathematician asked to boil the pot of water, who "reduced" the
problem to one already known rather than directly solve the easier
problem? So anyway, in Lisp you can do it in two steps, or in one step:
(loop for ix in (loop for ixx from 5 to 22 collect ixx) collect (expt ix 3))
(loop for ixx from 5 to 22 collect (expt ixx 3))
So how come python doesn't have the EXPT function? Or maybe it has
it but you didn't know about it, so you repeated 'x' three times in
a chain of multiplications because that's all you knew?

OK, I admit I'm being a bit hard on the newbie. No offense intended?
But really the newbie ought to ask questions using correct terminology.
NUMBER and INTEGER don't mean the same thing!!
That's what got me ticked off to give you the hard time. OK? Forgive me?

By the way, LOOP isn't a function, it's a macro-operator. The code
generated by expanding the macro-form evaluates some of LOOP's
apparent args more than once, some not at all, unlike functions
which get each arg evaluated exactly once before the function is
even called.

(macroexpand '(loop for ixx from 5 to 22 collect ixx))
breaks the prettyprinter in CMU Common Lisp 18b, jamming most of
the code so close to the right margin that linelength is exceeded
causing atoms to be split by line breaks. I could spend ten minutes
manually rearranging the 34 lines of spew to be more legible, but
it'd be better for you to do the macroexpand yourself for practice.

(macroexpand '(loop for ixx from 5 to 22 collect (expt ixx 3)))
breaks it in the same way, 36 lines of spew mostly at right margin.

XLISP-PLUS on the Mac doesn't even have the "new" LOOP macro
of ANSI CL, only the original LOOP special form from CLtL1,
not that anyone cares.
From: globalrev
Subject: Re: builtin for generating list?
Date: 
Message-ID: <66eaea3f-ae3f-4db8-b8f8-1b99c1cf167e@34g2000hsf.googlegroups.com>
On 14 Maj, 01:02, ···················@SpamGourmet.Com (Robert Maas,
http://tinyurl.com/uh3t) wrote:
> > From: globalrev <·········@yahoo.se>
> > if i want a list with the numbers between x and y
>
> That's not possible, because there are potentially an infinite
> number of (rational) numbers between any two given (rational)
> numbers. On the other hand, the set of *INTEGERS* between two
> (rational) numbers would be finite.
> Is that what you really meant to ask?
>
> > is there a buil [sic] in function to generate this?
>
> For *INTEGERS*, this is a trivial algorithm for you to write
> yourself using LOOP or DO or PROG or DOTIMES, so why do you care if
> there's a built-in function to do all your work with just one
> function call? It's not like you're going to waste six hours of
> work re-inventing something already in CL. So you waste half a
> minute re-inventing the function yourself. That's faster than
> composing a question to a newsgroup and waiting for a reply.
>
> > in python i can say range(x,y,step) so i can do:
> > map(lambda x:x*x*x,range(1,101,2)) which creates a list with all
> > the cubes of the odd numbers between 1 and 100.
>
> That's a stupid way to do it, building an explicit list of indices
> which will be used just to map to cubes, when you could do the
> whole thing in one step by directly looping over the range (without
> building a list of indices) to generate and collect the cubes.
> Of course if you already know how to generate the list of indices,
> and you know how to map over the indices, then it makes sense to
> combine the two steps rather than worry about doing the one-step
> method you don't know about. You know the joke about the
> mathematician asked to boil the pot of water, who "reduced" the
> problem to one already known rather than directly solve the easier
> problem? So anyway, in Lisp you can do it in two steps, or in one step:
> (loop for ix in (loop for ixx from 5 to 22 collect ixx) collect (expt ix 3))
> (loop for ixx from 5 to 22 collect (expt ixx 3))
> So how come python doesn't have the EXPT function? Or maybe it has
> it but you didn't know about it, so you repeated 'x' three times in
> a chain of multiplications because that's all you knew?
>
> OK, I admit I'm being a bit hard on the newbie. No offense intended?
> But really the newbie ought to ask questions using correct terminology.
> NUMBER and INTEGER don't mean the same thing!!
> That's what got me ticked off to give you the hard time. OK? Forgive me?
>
> By the way, LOOP isn't a function, it's a macro-operator. The code
> generated by expanding the macro-form evaluates some of LOOP's
> apparent args more than once, some not at all, unlike functions
> which get each arg evaluated exactly once before the function is
> even called.
>
> (macroexpand '(loop for ixx from 5 to 22 collect ixx))
> breaks the prettyprinter in CMU Common Lisp 18b, jamming most of
> the code so close to the right margin that linelength is exceeded
> causing atoms to be split by line breaks. I could spend ten minutes
> manually rearranging the 34 lines of spew to be more legible, but
> it'd be better for you to do the macroexpand yourself for practice.
>
> (macroexpand '(loop for ixx from 5 to 22 collect (expt ixx 3)))
> breaks it in the same way, 36 lines of spew mostly at right margin.
>
> XLISP-PLUS on the Mac doesn't even have the "new" LOOP macro
> of ANSI CL, only the original LOOP special form from CLtL1,
> not that anyone cares.


you can write x**3 instead of x*x*x if that is what u mean?



>> in python i can say range(x,y,step) so i can do:
>> map(lambda x:x*x*x,range(1,101,2)) which creates a list with all
>> the cubes of the odd numbers between 1 and 100.

>That's a stupid way to do it, building an explicit list of indices
>which will be used just to map to cubes, when you could do the
>whole thing in one step by directly looping over the range (without
>building a list of indices) to generate and collect the cubes.


well i can do:
f=[]
for i in range(1,101,2):
	if i%2 != 0:
		f=f+[i**3]

if u think that is better? map is a builtin function in python so it
could be faster even if it does more or uses more memory.
anyhow it shorter and more obvious what it does(ok both are obvious).
From: globalrev
Subject: Re: builtin for generating list?
Date: 
Message-ID: <d94daa24-7108-4881-889c-9a897ac02ec5@26g2000hsk.googlegroups.com>
On 14 Maj, 02:01, globalrev <·········@yahoo.se> wrote:
> On 14 Maj, 01:02, ···················@SpamGourmet.Com (Robert Maas,
>
>
>
> http://tinyurl.com/uh3t) wrote:
> > > From: globalrev <·········@yahoo.se>
> > > if i want a list with the numbers between x and y
>
> > That's not possible, because there are potentially an infinite
> > number of (rational) numbers between any two given (rational)
> > numbers. On the other hand, the set of *INTEGERS* between two
> > (rational) numbers would be finite.
> > Is that what you really meant to ask?
>
> > > is there a buil [sic] in function to generate this?
>
> > For *INTEGERS*, this is a trivial algorithm for you to write
> > yourself using LOOP or DO or PROG or DOTIMES, so why do you care if
> > there's a built-in function to do all your work with just one
> > function call? It's not like you're going to waste six hours of
> > work re-inventing something already in CL. So you waste half a
> > minute re-inventing the function yourself. That's faster than
> > composing a question to a newsgroup and waiting for a reply.
>
> > > in python i can say range(x,y,step) so i can do:
> > > map(lambda x:x*x*x,range(1,101,2)) which creates a list with all
> > > the cubes of the odd numbers between 1 and 100.
>
> > That's a stupid way to do it, building an explicit list of indices
> > which will be used just to map to cubes, when you could do the
> > whole thing in one step by directly looping over the range (without
> > building a list of indices) to generate and collect the cubes.
> > Of course if you already know how to generate the list of indices,
> > and you know how to map over the indices, then it makes sense to
> > combine the two steps rather than worry about doing the one-step
> > method you don't know about. You know the joke about the
> > mathematician asked to boil the pot of water, who "reduced" the
> > problem to one already known rather than directly solve the easier
> > problem? So anyway, in Lisp you can do it in two steps, or in one step:
> > (loop for ix in (loop for ixx from 5 to 22 collect ixx) collect (expt ix 3))
> > (loop for ixx from 5 to 22 collect (expt ixx 3))
> > So how come python doesn't have the EXPT function? Or maybe it has
> > it but you didn't know about it, so you repeated 'x' three times in
> > a chain of multiplications because that's all you knew?
>
> > OK, I admit I'm being a bit hard on the newbie. No offense intended?
> > But really the newbie ought to ask questions using correct terminology.
> > NUMBER and INTEGER don't mean the same thing!!
> > That's what got me ticked off to give you the hard time. OK? Forgive me?
>
> > By the way, LOOP isn't a function, it's a macro-operator. The code
> > generated by expanding the macro-form evaluates some of LOOP's
> > apparent args more than once, some not at all, unlike functions
> > which get each arg evaluated exactly once before the function is
> > even called.
>
> > (macroexpand '(loop for ixx from 5 to 22 collect ixx))
> > breaks the prettyprinter in CMU Common Lisp 18b, jamming most of
> > the code so close to the right margin that linelength is exceeded
> > causing atoms to be split by line breaks. I could spend ten minutes
> > manually rearranging the 34 lines of spew to be more legible, but
> > it'd be better for you to do the macroexpand yourself for practice.
>
> > (macroexpand '(loop for ixx from 5 to 22 collect (expt ixx 3)))
> > breaks it in the same way, 36 lines of spew mostly at right margin.
>
> > XLISP-PLUS on the Mac doesn't even have the "new" LOOP macro
> > of ANSI CL, only the original LOOP special form from CLtL1,
> > not that anyone cares.
>
> you can write x**3 instead of x*x*x if that is what u mean?
>
> >> in python i can say range(x,y,step) so i can do:
> >> map(lambda x:x*x*x,range(1,101,2)) which creates a list with all
> >> the cubes of the odd numbers between 1 and 100.
> >That's a stupid way to do it, building an explicit list of indices
> >which will be used just to map to cubes, when you could do the
> >whole thing in one step by directly looping over the range (without
> >building a list of indices) to generate and collect the cubes.
>
> well i can do:
> f=[]
> for i in range(1,101,2):
>         if i%2 != 0:
>                 f=f+[i**3]
>
> if u think that is better? map is a builtin function in python so it
> could be faster even if it does more or uses more memory.
> anyhow it shorter and more obvious what it does(ok both are obvious).

and befoe u say it, yes, "even if" is wrong. space and time, either or.
From: globalrev
Subject: Re: builtin for generating list?
Date: 
Message-ID: <3a455d4b-a557-4bb7-a5e1-207e4d20af4d@f63g2000hsf.googlegroups.com>
anyway,

if not to use map like this:
map(lambda x:x**3,xrange(1,101,2))

then when to use it?
From: Marco Gidde
Subject: Re: builtin for generating list?
Date: 
Message-ID: <83skwlzddx.fsf@tristan.br-automation.com>
globalrev <·········@yahoo.se> writes:

> anyway,
>
> if not to use map like this:
> map(lambda x:x**3,xrange(1,101,2))
>
> then when to use it?

Probably never if you ask GvR:

   [x**3 for x in xrange(1,101,2)] 

Not that I really like this syntax ...


Regards, Marco
From: globalrev
Subject: Re: builtin for generating list?
Date: 
Message-ID: <497b4555-6ee4-43d3-9cf6-52ab5a085cd9@27g2000hsf.googlegroups.com>
On 14 Maj, 07:02, Marco Gidde <···········@tiscali.de> wrote:
> globalrev <·········@yahoo.se> writes:
> > anyway,
>
> > if not to use map like this:
> > map(lambda x:x**3,xrange(1,101,2))
>
> > then when to use it?
>
> Probably never if you ask GvR:
>
>    [x**3 for x in xrange(1,101,2)]
>
> Not that I really like this syntax ...
>
> Regards, Marco

cool thanks.

but if u really want to use map the you would use it in the instance
before right?

or is there some more advanced way to use it?
From: Marco Gidde
Subject: Re: builtin for generating list?
Date: 
Message-ID: <83k5hxyuj5.fsf@tristan.br-automation.com>
globalrev <·········@yahoo.se> writes:
> On 14 Maj, 07:02, Marco Gidde <···········@tiscali.de> wrote:
>> globalrev <·········@yahoo.se> writes:
>> > anyway,
>>
>> > if not to use map like this:
>> > map(lambda x:x**3,xrange(1,101,2))
>>
>> > then when to use it?
>>
>> Probably never if you ask GvR:
>>
>>    [x**3 for x in xrange(1,101,2)]
>>
>> Not that I really like this syntax ...
>>
>> Regards, Marco
>
> cool thanks.
>
> but if u really want to use map the you would use it in the instance
> before right?

If you really want to, then that's the way to use it. But in Python
there is (or should be) always one obvious way to do things, and
nowadays it's list comprehension and not map.
From: Marco Gidde
Subject: Re: builtin for generating list?
Date: 
Message-ID: <83ej85yu5n.fsf@tristan.br-automation.com>
globalrev <·········@yahoo.se> writes:
> On 14 Maj, 07:02, Marco Gidde <···········@tiscali.de> wrote:
>> globalrev <·········@yahoo.se> writes:
>> > anyway,
>>
>> > if not to use map like this:
>> > map(lambda x:x**3,xrange(1,101,2))
>>
>> > then when to use it?
>>
>> Probably never if you ask GvR:
>>
>>    [x**3 for x in xrange(1,101,2)]
>>
>> Not that I really like this syntax ...
>>
>> Regards, Marco
>
> cool thanks.
>
> but if u really want to use map the you would use it in the instance
> before right?

If you really want to use map, then this is the right way, but in Python
there is or should be only one obvious way to do things, so in the end
you should not *want* to use map (nowadays).
From: Robert Maas, http://tinyurl.com/uh3t
Subject: Re: builtin for generating list?
Date: 
Message-ID: <rem-2008jun27-002@yahoo.com>
Why this response is so belated:
  <http://groups.google.com/group/misc.misc/msg/cea714440e591dd2>
= <······················@yahoo.com>
> From: globalrev <·········@yahoo.se>
> if not to use map like this:
> map(lambda x:x**3,xrange(1,101,2))
> then when to use it?

If your programming language provides a primitive or utility to
build a list of ascending integers (using RANGE, not XRANGE from
what I've read in this thread), and provides a primitive or utility
to map down a list applying a function to each element and
collecting the results in a second list, but doesn't provide a
primitive or utility to directly generate the ascending integers on
the fly and immediately apply the function and immediately collect
the result *without* building that list of ascending integers, then
then perhaps it's OK to write code that builds the list of
ascending integers then maps the function down it. It's my
understandign that the original article in this thread suggested
doing something equivalent in LISP, which several people said was
not the best way to accomplish the task.

But from what I read elsewhere in the thread, XRANGE produces an
iterator/continuation, and MAP accepts that to avoid making the
list of ascending integers, so your question above is moot to the
original question by the OP.

Reviewing the thread now, the OP (globalrev) asked:
   if i want a list with the numbers between x and y is there a buil in
   function to generate this?
Thus he wanted to first generate just the list of numbers between x and y.
   in python i can say range(x,y,step) so i can do:
   map(lambda x:x*x*x,range(1,101,2)) which creates a list with all the
   cubes of the odd numbers between 1 and 100.
Then he wanted to compound that with the mapping of function to
compute cube of each element in that list.
Clearly he was doing something bad there because he wasn't aware of
the xrange function to build an iterator/continutation and map's
ability to deal with that iterator in the expected way.

Later philip.armit... (Google Groups doesn't show the full name) advised:
   (defun range (start end &optional (step 1))
     (loop for n from start below end by step collect n))
   (mapcar (lambda (x) (* x x x)) (range 1 101 2))
Which is a direct translation of the bad code (unnecessarily
building the simple list of ascending integers) into Common Lisp.

Then I explained that you can do it in two steps (the bad way the
OP and Philip suggested):
 (loop for ix in (loop for ixx from 5 to 22 collect ixx) collect (expt ix 3))
or in one step (more efficient):
 (loop for ixx from 5 to 22 collect (expt ixx 3))
I think I contrasted the difference more succinctly than anybody else.

Note that more generally, it would be nice to have a standard
"interface" (Java jargon) that specifies how a source
iterator/continuation/stream behaves, and also how a sink-collector
anti-iterator/continuation/stream behaves, so that a mapping
function can be written that maps down the separate values
generated by that iteration, passes each through some function, and
feeds the results to the collector. This would be somewhat like a
generalization of how with-input-from-string and
with-output-to-string work, or how both the Enumeration and
Iterator interfaces work in Java as sources of data. Java doesn't
AFAIK have an interface for collectors, but several classes have a
no-arg constructor to start collecting and a single-argument method
obj.add(Object) which collects one new Object into the collection,
so that could be used as a sort of ad hoc interface for this
purpose, except that Java doesn't allow formal interfaces to be
specified after the fact, so hackery with the reflection
(self-introspection) API would be needed to build a
pseudo-interface. No wait, a given class that has both no-arg
constructor and add method could be sub-classed with no additional
functionality but formally declared to implement the interface. I
think that would work. Now getting back to Common Lisp: The same
trick should work in Lisp, except AFAIK you don't have compile-time
checks to make sure the interface really is satisied by the
sub-class.
From: Thomas A. Russ
Subject: Re: builtin for generating list?
Date: 
Message-ID: <ymiabisvael.fsf@blackcat.isi.edu>
globalrev <·········@yahoo.se> writes:

> On 14 Maj, 01:02, ···················@SpamGourmet.Com (Robert Maas,
> >That's a stupid way to do it, building an explicit list of indices
> >which will be used just to map to cubes, when you could do the
> >whole thing in one step by directly looping over the range (without
> >building a list of indices) to generate and collect the cubes.
> 
> 
> well i can do:
> f=[]
> for i in range(1,101,2):
> 	if i%2 != 0:
> 		f=f+[i**3]
> 
> if u think that is better? map is a builtin function in python so it
> could be faster even if it does more or uses more memory.
> anyhow it shorter and more obvious what it does(ok both are obvious).

No, the point is to use a built in iteration construct that means you
don't need to call the range function and construc a list of values in
the first place:

 f=[]
 for i from 1 to 101 by 2: f=f+[i**3]

Now, I'm not sure if Python has that particular syntax for its loops,
but Lisp sure does:

  (loop for i from 1 to 101 by 2 collect (* i i i))

So this way you only need to iterate once, instead of once to create the
list of integers and then a second time to go through that list and
create the cubes.


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Marco Antoniotti
Subject: Re: builtin for generating list?
Date: 
Message-ID: <9fc47e77-46b0-4808-95df-bd3a96ccca1e@25g2000hsx.googlegroups.com>
On May 14, 11:33 pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> globalrev <·········@yahoo.se> writes:
> > On 14 Maj, 01:02, ···················@SpamGourmet.Com (Robert Maas,
> > >That's a stupid way to do it, building an explicit list of indices
> > >which will be used just to map to cubes, when you could do the
> > >whole thing in one step by directly looping over the range (without
> > >building a list of indices) to generate and collect the cubes.
>
> > well i can do:
> > f=[]
> > for i in range(1,101,2):
> >    if i%2 != 0:
> >            f=f+[i**3]
>
> > if u think that is better? map is a builtin function in python so it
> > could be faster even if it does more or uses more memory.
> > anyhow it shorter and more obvious what it does(ok both are obvious).
>
> No, the point is to use a built in iteration construct that means you
> don't need to call the range function and construc a list of values in
> the first place:
>
>  f=[]
>  for i from 1 to 101 by 2: f=f+[i**3]
>
> Now, I'm not sure if Python has that particular syntax for its loops,
> but Lisp sure does:
>
>   (loop for i from 1 to 101 by 2 collect (* i i i))
>
> So this way you only need to iterate once, instead of once to create the
> list of integers and then a second time to go through that list and
> create the cubes.
>
> --
> Thomas A. Russ,  USC/Information Sciences Institute

Yes and no.  Python also has (or at least it did) an xrange operator
that is to be used mostly in loops which does not generate the list.
An equivalent in CL can be find in (shameless plug) CL-ENUMERATION :)
http://common-lisp.net/project/cl-enumeration/

As usual the problem lies in the non-extensibility (or very very, let
me repeat: very ugly extensibility) of LOOP.  It'd be very nice to be
able to write

    (loop for x over (enum:range 0 10 3) ; We cannot add "sub-
keywords" to LOOP.
          if (> x 5) do (return 42) else do (print x))
or
    (loop for x being each item of (enum:range 0 10 3) ; Try it, but
look at CL-SQL first. Very ugly.
          if (> x 5) do (return 42) else do (print x))


but you cannot (yet).  Unless somebody is willing to hack LOOP in the
way CL-SQL or LW-SQL has been hacked.

Any taker?

PS. The ITERATE version is ready and working.  I'll check it in ASAP.
It looks like

     (iterate (for x over (enum:range 0 10 5)) ...)

and yes.  Extending ITERATE is a charm.  The extension is just a few
simple and very understandable lines.

Cheers
--
Marco
From: Rainer Joswig
Subject: Re: builtin for generating list?
Date: 
Message-ID: <joswig-60FB72.09133415052008@news-europe.giganews.com>
In article 
<····································@25g2000hsx.googlegroups.com>,
 Marco Antoniotti <·······@gmail.com> wrote:

> On May 14, 11:33�pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> > globalrev <·········@yahoo.se> writes:
> > > On 14 Maj, 01:02, ···················@SpamGourmet.Com (Robert Maas,
> > > >That's a stupid way to do it, building an explicit list of indices
> > > >which will be used just to map to cubes, when you could do the
> > > >whole thing in one step by directly looping over the range (without
> > > >building a list of indices) to generate and collect the cubes.
> >
> > > well i can do:
> > > f=[]
> > > for i in range(1,101,2):
> > > � �if i%2 != 0:
> > > � � � � � �f=f+[i**3]
> >
> > > if u think that is better? map is a builtin function in python so it
> > > could be faster even if it does more or uses more memory.
> > > anyhow it shorter and more obvious what it does(ok both are obvious).
> >
> > No, the point is to use a built in iteration construct that means you
> > don't need to call the range function and construc a list of values in
> > the first place:
> >
> > �f=[]
> > �for i from 1 to 101 by 2: f=f+[i**3]
> >
> > Now, I'm not sure if Python has that particular syntax for its loops,
> > but Lisp sure does:
> >
> > � (loop for i from 1 to 101 by 2 collect (* i i i))
> >
> > So this way you only need to iterate once, instead of once to create the
> > list of integers and then a second time to go through that list and
> > create the cubes.
> >
> > --
> > Thomas A. Russ, �USC/Information Sciences Institute
> 
> Yes and no.  Python also has (or at least it did) an xrange operator
> that is to be used mostly in loops which does not generate the list.
> An equivalent in CL can be find in (shameless plug) CL-ENUMERATION :)
> http://common-lisp.net/project/cl-enumeration/
> 
> As usual the problem lies in the non-extensibility (or very very, let
> me repeat: very ugly extensibility) of LOOP.  It'd be very nice to be
> able to write
> 
>     (loop for x over (enum:range 0 10 3) ; We cannot add "sub-
> keywords" to LOOP.
>           if (> x 5) do (return 42) else do (print x))
> or
>     (loop for x being each item of (enum:range 0 10 3) ; Try it, but
> look at CL-SQL first. Very ugly.
>           if (> x 5) do (return 42) else do (print x))
> 
> 
> but you cannot (yet).  Unless somebody is willing to hack LOOP in the
> way CL-SQL or LW-SQL has been hacked.
> 
> Any taker?
> 
> PS. The ITERATE version is ready and working.  I'll check it in ASAP.
> It looks like
> 
>      (iterate (for x over (enum:range 0 10 5)) ...)
> 
> and yes.  Extending ITERATE is a charm.  The extension is just a few
> simple and very understandable lines.

IIRC, the MIT LOOP implementation also is extensible.
If Common Lisp's standard would be evolving there were alternatives:

a) let LOOP like it is and specify an ITERATE loop construct
b) specify an extensible LOOP
c) leave it like it is


> Cheers
> --
> Marco

-- 
http://lispm.dyndns.org/
From: Marco Antoniotti
Subject: Re: builtin for generating list?
Date: 
Message-ID: <f859969f-f08e-45f6-a186-4311649cc578@b64g2000hsa.googlegroups.com>
On May 15, 9:13 am, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@25g2000hsx.googlegroups.com>,
>  Marco Antoniotti <·······@gmail.com> wrote:
>
>
>
> > On May 14, 11:33 pm, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> > > globalrev <·········@yahoo.se> writes:
> > > > On 14 Maj, 01:02, ···················@SpamGourmet.Com (Robert Maas,
> > > > >That's a stupid way to do it, building an explicit list of indices
> > > > >which will be used just to map to cubes, when you could do the
> > > > >whole thing in one step by directly looping over the range (without
> > > > >building a list of indices) to generate and collect the cubes.
>
> > > > well i can do:
> > > > f=[]
> > > > for i in range(1,101,2):
> > > >    if i%2 != 0:
> > > >            f=f+[i**3]
>
> > > > if u think that is better? map is a builtin function in python so it
> > > > could be faster even if it does more or uses more memory.
> > > > anyhow it shorter and more obvious what it does(ok both are obvious).
>
> > > No, the point is to use a built in iteration construct that means you
> > > don't need to call the range function and construc a list of values in
> > > the first place:
>
> > >  f=[]
> > >  for i from 1 to 101 by 2: f=f+[i**3]
>
> > > Now, I'm not sure if Python has that particular syntax for its loops,
> > > but Lisp sure does:
>
> > >   (loop for i from 1 to 101 by 2 collect (* i i i))
>
> > > So this way you only need to iterate once, instead of once to create the
> > > list of integers and then a second time to go through that list and
> > > create the cubes.
>
> > > --
> > > Thomas A. Russ,  USC/Information Sciences Institute
>
> > Yes and no.  Python also has (or at least it did) an xrange operator
> > that is to be used mostly in loops which does not generate the list.
> > An equivalent in CL can be find in (shameless plug) CL-ENUMERATION :)
> >http://common-lisp.net/project/cl-enumeration/
>
> > As usual the problem lies in the non-extensibility (or very very, let
> > me repeat: very ugly extensibility) of LOOP.  It'd be very nice to be
> > able to write
>
> >     (loop for x over (enum:range 0 10 3) ; We cannot add "sub-
> > keywords" to LOOP.
> >           if (> x 5) do (return 42) else do (print x))
> > or
> >     (loop for x being each item of (enum:range 0 10 3) ; Try it, but
> > look at CL-SQL first. Very ugly.
> >           if (> x 5) do (return 42) else do (print x))
>
> > but you cannot (yet).  Unless somebody is willing to hack LOOP in the
> > way CL-SQL or LW-SQL has been hacked.
>
> > Any taker?
>
> > PS. The ITERATE version is ready and working.  I'll check it in ASAP.
> > It looks like
>
> >      (iterate (for x over (enum:range 0 10 5)) ...)
>
> > and yes.  Extending ITERATE is a charm.  The extension is just a few
> > simple and very understandable lines.
>
> IIRC, the MIT LOOP implementation also is extensible.

Yes, it is in a very ugly, bad looking, gut twisting way :)

> If Common Lisp's standard would be evolving there were alternatives:
>
> a) let LOOP like it is and specify an ITERATE loop construct
> b) specify an extensible LOOP
> c) leave it like it is

Sure.  However, my feeling is that settling on an agreed upon LOOP
extensibility framework would be slightly better at this point.

Cheers
--
Marco
From: Rainer Joswig
Subject: Re: builtin for generating list?
Date: 
Message-ID: <joswig-065978.13274815052008@news-europe.giganews.com>
In article 
<····································@b64g2000hsa.googlegroups.com>,
 Marco Antoniotti <·······@gmail.com> wrote:

> > > and yes. �Extending ITERATE is a charm. �The extension is just a few
> > > simple and very understandable lines.
> >
> > IIRC, the MIT LOOP implementation also is extensible.
> 
> Yes, it is in a very ugly, bad looking, gut twisting way :)

Here is the doc of the corresponding Lisp Machine Lisp version
of LOOP:

http://common-lisp.net/project/bknr/static/lmman/looptm.xml#defstruct-section

> 
> > If Common Lisp's standard would be evolving there were alternatives:
> >
> > a) let LOOP like it is and specify an ITERATE loop construct
> > b) specify an extensible LOOP
> > c) leave it like it is
> 
> Sure.  However, my feeling is that settling on an agreed upon LOOP
> extensibility framework would be slightly better at this point.
> 
> Cheers
> --
> Marco

-- 
http://lispm.dyndns.org/
From: globalrev
Subject: Re: builtin for generating list?
Date: 
Message-ID: <e905d4ff-7722-47a2-8f6b-83fde81ed704@a1g2000hsb.googlegroups.com>
On 14 Maj, 23:33, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> globalrev <·········@yahoo.se> writes:
> > On 14 Maj, 01:02, ···················@SpamGourmet.Com (Robert Maas,
> > >That's a stupid way to do it, building an explicit list of indices
> > >which will be used just to map to cubes, when you could do the
> > >whole thing in one step by directly looping over the range (without
> > >building a list of indices) to generate and collect the cubes.
>
> > well i can do:
> > f=[]
> > for i in range(1,101,2):
> >    if i%2 != 0:
> >            f=f+[i**3]
>
> > if u think that is better? map is a builtin function in python so it
> > could be faster even if it does more or uses more memory.
> > anyhow it shorter and more obvious what it does(ok both are obvious).
>
> No, the point is to use a built in iteration construct that means you
> don't need to call the range function and construc a list of values in
> the first place:
>
>  f=[]
>  for i from 1 to 101 by 2: f=f+[i**3]
>
> Now, I'm not sure if Python has that particular syntax for its loops,
> but Lisp sure does:
>
>   (loop for i from 1 to 101 by 2 collect (* i i i))
>
> So this way you only need to iterate once, instead of once to create the
> list of integers and then a second time to go through that list and
> create the cubes.
>
> --
> Thomas A. Russ,  USC/Information Sciences Institute


[x**3 for x in xrange(1,101,2)] isnt that what it does?
From: danb
Subject: Re: builtin for generating list?
Date: 
Message-ID: <3b1ad07b-9be0-4208-b7e9-84a381d8db4e@m36g2000hse.googlegroups.com>
> On 14 Maj, 23:33, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> >   (loop for i from 1 to 101 by 2 collect (* i i i))

On May 15, 5:11 am, globalrev <·········@yahoo.se> wrote:
> [x**3 for x in xrange(1,101,2)] isnt that what it does?

Basically, but the details depend on the implementation.
The main question in terms of performance is what Python
does with xrange.  Does it construct a list of numbers and
then cube them?  Does it construct a second list for the
cubes?  Or does the compiler know how to optimize the code
by cubing the numbers first?  Unless the compiler knows
how to optimize the code, the mapping implementation will
be slow and will take up more space, because it constructs
two lists.  To avoid that, you would have to construct
the second list out of the first (after defining xrange):

(mapl (lambda (cell)
        (setf (car cell) (expt (car cell) 3)))
      (xrange 1 101 2))

(untested)

but it still traverses the list twice, and LOOP
alledgedly avoids a lot of extra work by consing
directly onto the end of the list.

--Dan

------------------------------------------------
Dan Bensen  http://www.prairienet.org/~dsb/

cl-match:  expressive pattern matching in Lisp
http://common-lisp.net/project/cl-match/
From: Tung Nguyen
Subject: Re: builtin for generating list?
Date: 
Message-ID: <78f2406c-8f08-46f4-801d-9bf234b0b0e1@q1g2000prf.googlegroups.com>
On May 15, 10:00 pm, danb <·········@gmail.com> wrote:
> > On 14 Maj, 23:33, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> > >   (loop for i from 1 to 101 by 2 collect (* i i i))
>
> On May 15, 5:11 am, globalrev <·········@yahoo.se> wrote:
>
> > [x**3 for x in xrange(1,101,2)] isnt that what it does?
>
> Basically, but the details depend on the implementation.
> The main question in terms of performance is what Python
> does with xrange.

Python's range makes a list.  xrange returns a generator which yields
the values you're after as they're requested.  According to the
language's library reference, "an xrange object will always take the
same amount of memory, no matter the size of the range it represents.
There are no consistent performance advantages."  Make of it as you
will.
From: John Thingstad
Subject: Re: builtin for generating list?
Date: 
Message-ID: <op.ua9begp2ut4oq5@pandora.alfanett.no>
P� Fri, 16 May 2008 16:14:01 +0200, skrev Tung Nguyen  
<·········@gmail.com>:

> On May 15, 10:00 pm, danb <·········@gmail.com> wrote:
>> > On 14 Maj, 23:33, ····@sevak.isi.edu (Thomas A. Russ) wrote:
>> > >   (loop for i from 1 to 101 by 2 collect (* i i i))
>>
>> On May 15, 5:11 am, globalrev <·········@yahoo.se> wrote:
>>
>> > [x**3 for x in xrange(1,101,2)] isnt that what it does?
>>
>> Basically, but the details depend on the implementation.
>> The main question in terms of performance is what Python
>> does with xrange.
>
> Python's range makes a list.  xrange returns a generator which yields
> the values you're after as they're requested.  According to the
> language's library reference, "an xrange object will always take the
> same amount of memory, no matter the size of the range it represents.
> There are no consistent performance advantages."  Make of it as you
> will.

You can acieve the same by using the series package.
http://series.sourceforge.net/

On the whole a more efficient solution than the map family.
Like Haskel it uses lazy evaluation to compute values on demand and  
serializes commands.

Example:

(LET ((X (SUBSERIES (SCAN-RANGE :FROM 0 :BY 2) 0 5)))
   (VALUES (COLLECT X) (COLLECT-SUM X))) => (0 2 4 6 8) and 20

--------------
John Thingstad
From: Marco Antoniotti
Subject: Re: builtin for generating list?
Date: 
Message-ID: <179dce51-9216-4084-8f7e-58df452a2ceb@27g2000hsf.googlegroups.com>
On May 16, 1:12 pm, "John Thingstad" <·······@online.no> wrote:
> På Fri, 16 May 2008 16:14:01 +0200, skrev Tung Nguyen
> <·········@gmail.com>:
>
>
>
> > On May 15, 10:00 pm, danb <·········@gmail.com> wrote:
> >> > On 14 Maj, 23:33, ····@sevak.isi.edu (Thomas A. Russ) wrote:
> >> > >   (loop for i from 1 to 101 by 2 collect (* i i i))
>
> >> On May 15, 5:11 am, globalrev <·········@yahoo.se> wrote:
>
> >> > [x**3 for x in xrange(1,101,2)] isnt that what it does?
>
> >> Basically, but the details depend on the implementation.
> >> The main question in terms of performance is what Python
> >> does with xrange.
>
> > Python's range makes a list.  xrange returns a generator which yields
> > the values you're after as they're requested.  According to the
> > language's library reference, "an xrange object will always take the
> > same amount of memory, no matter the size of the range it represents.
> > There are no consistent performance advantages."  Make of it as you
> > will.
>
> You can acieve the same by using the series package.http://series.sourceforge.net/
>
> On the whole a more efficient solution than the map family.
> Like Haskel it uses lazy evaluation to compute values on demand and
> serializes commands.
>
> Example:
>
> (LET ((X (SUBSERIES (SCAN-RANGE :FROM 0 :BY 2) 0 5)))
>    (VALUES (COLLECT X) (COLLECT-SUM X))) => (0 2 4 6 8) and 20
>
> --------------
> John Thingstad

Or the - shameless plug - CL-ENUMERATION package. :)

(use-package "ENUM")

(foreach (x (range 1 101 2)) collect (expt x 3))

If you want sets and tuples look at the result of googling groups with
"SETL Lisp"

http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/e84f21a6fdabc3c5/b02df63f20192570?hl=en&lnk=st&q=setl+lisp+group%3Acomp.lang.lisp#b02df63f20192570

Cheers
--
Marco
From: Patrick Stein
Subject: Re: builtin for generating list?
Date: 
Message-ID: <3b8c7bc7-f3b5-492c-9063-fba0df7b719c@a70g2000hsh.googlegroups.com>
On May 13, 6:02 pm, ···················@SpamGourmet.Com (Robert Maas,
http://tinyurl.com/uh3t) wrote:
> That's a stupid way to do it, building an explicitlistof indices
> which will be used just to map to cubes, when you could do the
> whole thing in one step by directly looping over the range (without
> building alistof indices) to generate and collect the cubes.

To be fair, the OP's Python would not have generated the explicit list
of indices.
The range() function returns an iterator.  The map function would not
have created
an exhaustive list before starting to invoke the lambdas and collect
them.  The
Python map( lambda x:x*x*x, range(1,101,2) ) would have been exactly
like the LOOP
version proposed by many in this thread.

And, while Python has a pow() function, it is explicitly floating
point.  So, x*x*x
would keep things integer.

ttyl,
Patrick
From: Robert Maas, http://tinyurl.com/uh3t
Subject: Re: builtin for generating list?
Date: 
Message-ID: <rem-2008jul20-001@yahoo.com>
> From: Patrick Stein <····@csh.rit.edu>
> the OP's Python would not have generated the explicit list of
> indices. The range() function returns an iterator.

I can't say for sure, becase I've never used Python, but from
reading the discussion it's my understanding that the range()
function *would* build an explicit list whereas the xrange()
notation would build an iterator and pipe it through the enclosing
map().

But my original complaint, IIRC, was when the map of range was
translated to Lisp, where it most definitely *would* build a list
and then map down it. I showed how in *Lisp* it ought to be done
differently if at all possible.

> And, while Python has a pow() function, it is explicitly floating
> point.  So, x*x*x would keep things integer.

Again, I don't know Python, so I wasn't aware of that, and any
comments I made about the Lisp translation apply *only* to the Lisp
code where (expt int posint) most definitely keeps everything
integer. But thanks for the curious bit of trivia about Python
which would be good to know if I ever want to use Python for any
reason.

By the way, so-far nobody has created a live CGI demo of Python
doing full decoding of URLENCODED-form-contents. To your knowledge,
is Python even capable of reading the environment variables used to
communicate the GET string or the POST length in CGI? If so, and if
you wish people to take you seriously when you advocate use of
Python for any purpose, would you please create a live Python
CGI-hello-plus-three-steps demo and show me where to find it so
that I can link it into my collection?

So-far, all I have for Python is the basic static-HTML-output
hello-world demo:
  <http://www.rawbw.com/~rem/HelloPlus/hellos.html#python0>
and the variable-output that cannot respond to either query string
or post data:
  <http://www.rawbw.com/~rem/HelloPlus/hellos.html#python1>
Would you please construct the Python equivalent of the next two
steps after that:
  <http://www.rawbw.com/~rem/HelloPlus/hellos.html#step2>
    (responsive, output depends on user input, currently
     demonstrated in: php sh* perl lisp awk c c++ ft java)
  <http://www.rawbw.com/~rem/HelloPlus/hellos.html#step3>
     (proper decoding of HTML-form contents to some sort of lookup
      table i.e. "associative array", so that program can then
      correctly respond to user input, currently demonstrated in:
      php perl lisp c c++ java ft)

I'm sure that since I already have the variable output that
includes the value of the PATH variable, it would take only a
little bit of extra code to check the REQUEST_METHOD and then
decide whether to decode the QUERY_STRING or the CONTENT_LENGTH
plus contents of standard input, and then to perform some simple
sub-string test to satisfy the step2 requirements. (I just don't
have time/energy/incentive to study the Python tutorials to figure
out how to do this myself.) So I'm guessing that *you* would find
that task trivial, and you'll do it promptly, right?

As for full decoding of form contents per step3, that may take a
bit of serious software to get the job done properly, possibly by
translating the sample demo code from Lisp where *everything* is
done explicitly or Java where all code above the URL decoder is
done explicitly, so if you aren't capable of the task I would
understand.

(Note that in perl and PHP, all the work is done automatically by
 the programming environment, while in C and C++ I used an elaborate
 CGI-form-decoding module that I found on the net so it's probably
 overkill for this kind of demo, and in Flaming Thunder some of the
 form-decoding stuff is done already by the programming environment.
 Really Lisp, and perhaps Java, are the only languages where
 most/all of the decoding is explicit in the code I wrote so that
 you can probably look at my code and directly translate to
 whatever your favorite programming language might be.)
From: Leslie P. Polzer
Subject: Re: builtin for generating list?
Date: 
Message-ID: <a4764b4a-0fae-4fae-a942-47553147d69c@8g2000hse.googlegroups.com>
On May 13, 11:26 pm, globalrev <·········@yahoo.se> wrote:
> if i want a list with the numbers between x and y is there a buil in
> function to generate this?

Look below, but you probably want to use the SERIES add-on.

> in python i can say range(x,y,step) so i can do:
> map(lambda x:x*x*x,range(1,101,2)) which creates a list with all the
> cubes of the odd numbers between 1 and 100.

(asdf-install:install 'series)
(asdf:oos 'asdf:load-op 'series)
(use-package 'series)

(scan-range :from 1 :upto 7)
  -> #Z(1 2 3 4 5 6 7) ; a series object

(collect *)
  -> (1 2 3 4 5 6 7) ; an ordinary list

Now use MAPCAR or whatever, or map the series object directly,
see http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node351.html#SECTION003422000000000000000.