[racket] Help with filter-map

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Mon May 28 02:48:05 EDT 2012

One of the problems with this is that "(stream->list (in-range 2 
1000000))" is constructing a list of approx. 1 million elements just to 
count a number by 1.  Counting a number by 1 can instead be done by: (+ 
1 number)

The fancy iterators like "for" can do secret tricks to make things like 
"(in-range 2 1000000)" be reasonable to use in Racket.  But 
"stream->list" cannot do these secret tricks, since it really does 
construct a non-lazy list of approx. a million elements.

Two alternatives:

1. One can use Racket like a mathematician who uses umpteen operators to 
make umpteen lists of arbitrary size, since everything is declarative 
and functional and pure and free.  Performance is not an issue, since 
the universe of this mathematician has no dimension of time.

2. One can care about runtime performance, and treat Racket to a large 
extent as a conventional algorithmic programming language for the 
conventional architecture.  I think this is easier to learn if one does 
things like: (1) mothballs Project Euler and focuses on problems better 
suited to learning the fundamentals of Racket; (2) avoids using Racket 
sequences and iterators and fancy list functions and such, until 
understanding how one would do it efficiently without those things; and 
(3) is skeptical of anything that would look good to a mathematician, 
since the way of the mathematician is the way of lies.

BTW, line breaks would improve the readability of the code, by visually 
exposing more of the nesting structure.

Neil V.

Posted on the users mailing list.