Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of Clojure's iterate function in Racket

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.

like image 400
interstar Avatar asked Oct 03 '15 00:10

interstar


1 Answers

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)
like image 91
Chris Jester-Young Avatar answered Oct 14 '22 13:10

Chris Jester-Young