[racket-dev] exact nonnegative integers as sequences?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Apr 18 11:11:40 EDT 2011

At Mon, 18 Apr 2011 11:04:18 -0400, Eli Barzilay wrote:
> An hour and a half ago, Matthew Flatt wrote:
> > I often write
> > 
> >  (for.... ([i (in-range N)]) ...)
> > 
> > In cases where the loop overhead is not significant (i.e., I don't
> > care whether the compiler can tell that I'm iterating through
> > integers), it would be nice to write just
> What's the overhead?

The same as using `(in-range N)' as a value: an indirection on the
`(curry = N)' test, `add1' increment, and identity conversion from the
loop index to the loop value.

 #lang racket

 (define N 1000000)
 (define N-range (in-range N))

 (time (for/fold ([v 0]) ([i (in-range N)])
 ; cpu time: 4 real time: 3 gc time: 0

 (time (for/fold ([v 0]) ([i N-range])
 ; cpu time: 103 real time: 103 gc time: 0

You have to be doing very little work in the loop body for the
100-nanosecond-per-iteration overhead to matter, though.

Posted on the dev mailing list.