4
Racket中是否存在類似Python的itertools.count
?我想創建一個無限均勻分佈的數字流。 in-naturals
與我想要的類似,但不提供步驟。我不想重新發明輪子,但如果沒有相同的功能,如何寫一個? (我想,發電機應使用)Racket中的無限升序排列
Racket中是否存在類似Python的itertools.count
?我想創建一個無限均勻分佈的數字流。 in-naturals
與我想要的類似,但不提供步驟。我不想重新發明輪子,但如果沒有相同的功能,如何寫一個? (我想,發電機應使用)Racket中的無限升序排列
製作功能你自己可以在一條線上完成:
(define (stream-from n s) (stream-cons n (stream-from (+ n s) s)))
要測試它,這裏是一個打印100000個數字的示例:
#lang racket
(require racket/stream)
(define (stream-from n s) (stream-cons n (stream-from (+ n s) s)))
(define (stream-while s p)
(let ([fst (stream-first s)])
(if (p fst) (stream-cons fst (stream-while (stream-rest s) p)) empty-stream)))
(define test (stream-while (stream-from 0 1) (λ (x) (< x 100000))))
(stream-for-each println test)