@@ -105,7 +105,13 @@ You can define procedures that operate on streams, like procedure that add two s
105
105
(add-streams (stream-cdr s1) (stream-cdr s2)))))
106
106
```
107
107
108
- You can sue this function to create stream of integers:
108
+ ::: info
109
+
110
+ This function don't check if any of the streams are not empty. So it will only work on infnite streams.
111
+
112
+ :::
113
+
114
+ You can use this function to create stream of integers:
109
115
110
116
``` scheme
111
117
(define integers (stream-cons 1 (stream-add integers ones)))
@@ -136,10 +142,22 @@ creates a single list in output stream.
136
142
137
143
``` scheme
138
144
(define (stream-zip . streams)
139
- (if (empty-stream? streams)
145
+ (if (or (null? streams) (some empty-stream? streams) )
140
146
the-empty-stream
141
147
(stream-cons (apply list (map stream-car streams))
142
148
(apply stream-zip (map stream-cdr streams)))))
149
+
150
+ (define (some fn lists)
151
+ (if (or (null? lists) (some/1 null? lists))
152
+ #f
153
+ (or (apply fn (map car lists))
154
+ (some fn (map cdr lists)))))
155
+
156
+ (define (some/1 fn lst)
157
+ (if (fn lst)
158
+ #f
159
+ (or (fn (car lst))
160
+ (some/1 (cdr lst)))))
143
161
```
144
162
145
163
The function work like this:
@@ -156,7 +174,7 @@ With map, you can simplify `stream-add` with `stream-map`:
156
174
(stream-map + s1 s2))
157
175
```
158
176
159
- Another useful procedures that accept stream are stream-limit and force-stream:
177
+ Another useful procedures that accept stream are ` stream-limit ` and ` force-stream ` :
160
178
161
179
``` scheme
162
180
(define (stream-limit n stream)
@@ -175,11 +193,14 @@ Another useful procedures that accept stream are stream-limit and force-stream:
175
193
(loop (stream-cdr stream))))))
176
194
```
177
195
196
+ ::: info
197
+
178
198
If you call ` force-stream ` on infinite stream it will create infinite loop, but note that the
179
199
procedure force-stream is not tail recursive. The recursive call to named ` let ` is not the last
180
- expression. The last expression is ` cons ` .
200
+ expression. The last expression is ` cons ` , so it will throw stack overflow error .
181
201
182
202
You can try to create a tail recursive version of the procedure as an exercise.
203
+ :::
183
204
184
205
If you combine both procedures, you can create the same effect as with ` stream-take ` :
185
206
@@ -188,7 +209,7 @@ If you combine both procedures, you can create the same effect as with `stream-t
188
209
;; ==> (1 2 3 4 5 6 7 8 9 10)
189
210
```
190
211
191
- So you can implement stream-take with above procedures:
212
+ So you can implement ` stream-take ` with above procedures:
192
213
193
214
``` scheme
194
215
(define (stream-take n stream)
@@ -198,7 +219,7 @@ So you can implement stream-take with above procedures:
198
219
;; ==> (1 2 3 4 5 6 7 8 9 10)
199
220
```
200
221
201
- Another useful procedure is ` stream-reduce ` in Scheme often called ` fold ` :
222
+ Another useful procedure is ` stream-reduce ` :
202
223
203
224
``` scheme
204
225
(define (stream-reduce fun stream)
0 commit comments