Search code examples
streamschemeracketinfinite-sequence

Infinite ascending sequence in Racket


Is there an analog of Python's itertools.count in Racket? I want to create an infinite stream of evenly spaced numbers. in-naturals is similar to what i want, but does not provide step. I'd want not to reinvent the wheel, but if there's no equivalent function, how to write one? (i presume, generators should be used)


Solution

  • You can get the same functionality of Python's count using in-range with an infinite end value:

    (define (count start step)
      (in-range start +inf.0 step))
    

    For example:

    (define s (count 2.5 0.5))
    
    (stream-ref s 0)
    => 2.5
    (stream-ref s 1)
    => 3.0
    (stream-ref s 2)
    => 3.5
    (stream-ref s 3)
    => 4.0