Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 810
Mirzhan Irkegulov Avatar asked Feb 18 '23 11:02

Mirzhan Irkegulov


1 Answers

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
like image 156
Óscar López Avatar answered Mar 04 '23 23:03

Óscar López