Streams in a scheme - determination of integers through a stream map in a scheme

How can I define integers through a flow map in a Scheme:

  (define integers (stream-cons 1 (stream-map * something * * something *)) 
+4
source share
3 answers

The response from @ user448810 is perfect, and it will work in Racket (it uses the procedures specific to Racket). But the question is also marked by SICP , so here are my two cents.

Answering the question only with the help of a subset of the Schema procedures available in SICP, an equivalent but slightly different solution is obtained using only the following primitive stream operations defined in the book: stream-null? stream-cons stream-car stream-cdr stream-null? stream-cons stream-car stream-cdr . In particular, note that stream-map not a standard part of the Scheme, and in the book it is implemented in terms of primitive operations with an advantage over the Racket implementation - it can accept a variable number of streams as parameters:

 (define (stream-map proc . args) (if (stream-null? (car args)) stream-null (stream-cons (apply proc (map stream-car args)) (apply stream-map (cons proc (map stream-cdr args)))))) (define (add-streams s1 s2) (stream-map + s1 s2)) 

Using the procedures described above, it is easy to define integers

 (define ones (stream-cons 1 ones)) (define integers (stream-cons 1 (add-streams ones integers))) 
+4
source
 (define integers (stream-cons 1 (stream-map add1 integers))) 

For more information on streams, see SRFI-41 .

+3
source

You can also use the example from SICP:

 (define (integers-starting-from n) (cons-stream n (integers-starting-from (+ n 1)))) (define nats (integers-starting-from 1)) 
0
source

All Articles