2013-02-20 49 views

回答

6

你可以得到相同的功能Python的使用in-range具有無限end價值count

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

例如:

(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 
1

製作功能你自己可以在一條線上完成:

(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)