I'm playing with Racket today, and trying to produce an indefinite sequence of numbers based on multiple applications of the same function.
In Clojure I'd use the iterate function for this, but I'm not sure what would be the equivalent in Racket.
SRFI 41 ((require srfi/41)
) provides stream-iterate
directly.
You can use Óscar's examples and substitute stream-iterate
wherever you see iterate
, without having to define your own iterate
. In addition, you can simulate Clojure's parameter destructuring using match-lambda
:
(require srfi/41)
(define fib
(stream-map first
(stream-iterate (match-lambda
((list a b)
(list b (+ a b))))
'(0 1))))
(stream->list 10 fib) ; => (0 1 1 2 3 5 8 13 21 34)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With