Skip to content

Commit 6b68081

Browse files
committed
(docs) fixes to stream page
1 parent 4430bc1 commit 6b68081

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

docs/docs/scheme-intro/streams.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ You can define procedures that operate on streams, like procedure that add two s
105105
(add-streams (stream-cdr s1) (stream-cdr s2)))))
106106
```
107107

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:
109115

110116
```scheme
111117
(define integers (stream-cons 1 (stream-add integers ones)))
@@ -136,10 +142,22 @@ creates a single list in output stream.
136142

137143
```scheme
138144
(define (stream-zip . streams)
139-
(if (empty-stream? streams)
145+
(if (or (null? streams) (some empty-stream? streams))
140146
the-empty-stream
141147
(stream-cons (apply list (map stream-car streams))
142148
(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)))))
143161
```
144162

145163
The function work like this:
@@ -156,7 +174,7 @@ With map, you can simplify `stream-add` with `stream-map`:
156174
(stream-map + s1 s2))
157175
```
158176

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`:
160178

161179
```scheme
162180
(define (stream-limit n stream)
@@ -175,11 +193,14 @@ Another useful procedures that accept stream are stream-limit and force-stream:
175193
(loop (stream-cdr stream))))))
176194
```
177195

196+
:::info
197+
178198
If you call `force-stream` on infinite stream it will create infinite loop, but note that the
179199
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.
181201

182202
You can try to create a tail recursive version of the procedure as an exercise.
203+
:::
183204

184205
If you combine both procedures, you can create the same effect as with `stream-take`:
185206

@@ -188,7 +209,7 @@ If you combine both procedures, you can create the same effect as with `stream-t
188209
;; ==> (1 2 3 4 5 6 7 8 9 10)
189210
```
190211

191-
So you can implement stream-take with above procedures:
212+
So you can implement `stream-take` with above procedures:
192213

193214
```scheme
194215
(define (stream-take n stream)
@@ -198,7 +219,7 @@ So you can implement stream-take with above procedures:
198219
;; ==> (1 2 3 4 5 6 7 8 9 10)
199220
```
200221

201-
Another useful procedure is `stream-reduce` in Scheme often called `fold`:
222+
Another useful procedure is `stream-reduce`:
202223

203224
```scheme
204225
(define (stream-reduce fun stream)

0 commit comments

Comments
 (0)