| 1 |
#!r6rs |
| 2 |
;;; Copyright (C) Philip L. Bewig (2007). All Rights Reserved. |
| 3 |
;;; Permission is hereby granted, free of charge, to any person |
| 4 |
;;; obtaining a copy of this software and associated documentation files |
| 5 |
;;; (the "Software"), to deal in the Software without restriction, |
| 6 |
;;; including without limitation the rights to use, copy, modify, merge, |
| 7 |
;;; publish, distribute, sublicense, and/or sell copies of the Software, |
| 8 |
;;; and to permit persons to whom the Software is furnished to do so, |
| 9 |
;;; subject to the following conditions: |
| 10 |
;;; |
| 11 |
;;; The above copyright notice and this permission notice shall be |
| 12 |
;;; included in all copies or substantial portions of the Software. |
| 13 |
;;; |
| 14 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 |
;;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
;;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
;;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
;;; SOFTWARE. |
| 22 |
|
| 23 |
(library (srfi :41 streams derived) |
| 24 |
|
| 25 |
(export stream-null stream-cons stream? stream-null? stream-pair? stream-car |
| 26 |
stream-cdr stream-lambda define-stream list->stream port->stream stream |
| 27 |
stream->list stream-append stream-concat stream-constant stream-drop |
| 28 |
stream-drop-while stream-filter stream-fold stream-for-each stream-from |
| 29 |
stream-iterate stream-length stream-let stream-map stream-match |
| 30 |
stream-of stream-range stream-ref stream-reverse stream-scan stream-take |
| 31 |
stream-take-while stream-unfold stream-unfolds stream-zip) |
| 32 |
|
| 33 |
(import (rnrs) (srfi :41 streams primitive)) |
| 34 |
|
| 35 |
(define-syntax define-stream |
| 36 |
(syntax-rules () |
| 37 |
((define-stream (name . formal) body0 body1 ...) |
| 38 |
(define name (stream-lambda formal body0 body1 ...))))) |
| 39 |
|
| 40 |
(define (list->stream objs) |
| 41 |
(define list->stream |
| 42 |
(stream-lambda (objs) |
| 43 |
(if (null? objs) |
| 44 |
stream-null |
| 45 |
(stream-cons (car objs) (list->stream (cdr objs)))))) |
| 46 |
(if (not (list? objs)) |
| 47 |
(error 'list->stream "non-list argument") |
| 48 |
(list->stream objs))) |
| 49 |
|
| 50 |
(define (port->stream . port) |
| 51 |
(define port->stream |
| 52 |
(stream-lambda (p) |
| 53 |
(let ((c (read-char p))) |
| 54 |
(if (eof-object? c) |
| 55 |
stream-null |
| 56 |
(stream-cons c (port->stream p)))))) |
| 57 |
(let ((p (if (null? port) (current-input-port) (car port)))) |
| 58 |
(if (not (input-port? p)) |
| 59 |
(error 'port->stream "non-input-port argument") |
| 60 |
(port->stream p)))) |
| 61 |
|
| 62 |
(define-syntax stream |
| 63 |
(syntax-rules () |
| 64 |
((stream) stream-null) |
| 65 |
((stream x y ...) (stream-cons x (stream y ...))))) |
| 66 |
|
| 67 |
(define (stream->list . args) |
| 68 |
(let ((n (if (= 1 (length args)) #f (car args))) |
| 69 |
(strm (if (= 1 (length args)) (car args) (cadr args)))) |
| 70 |
(cond ((not (stream? strm)) (error 'stream->list "non-stream argument")) |
| 71 |
((and n (not (integer? n))) (error 'stream->list "non-integer count")) |
| 72 |
((and n (negative? n)) (error 'stream->list "negative count")) |
| 73 |
(else (let loop ((n (if n n -1)) (strm strm)) |
| 74 |
(if (or (zero? n) (stream-null? strm)) |
| 75 |
'() |
| 76 |
(cons (stream-car strm) (loop (- n 1) (stream-cdr strm))))))))) |
| 77 |
|
| 78 |
(define (stream-append . strms) |
| 79 |
(define stream-append |
| 80 |
(stream-lambda (strms) |
| 81 |
(cond ((null? (cdr strms)) (car strms)) |
| 82 |
((stream-null? (car strms)) (stream-append (cdr strms))) |
| 83 |
(else (stream-cons (stream-car (car strms)) |
| 84 |
(stream-append (cons (stream-cdr (car strms)) (cdr strms)))))))) |
| 85 |
(cond ((null? strms) stream-null) |
| 86 |
((exists (lambda (x) (not (stream? x))) strms) |
| 87 |
(error 'stream-append "non-stream argument")) |
| 88 |
(else (stream-append strms)))) |
| 89 |
|
| 90 |
(define (stream-concat strms) |
| 91 |
(define stream-concat |
| 92 |
(stream-lambda (strms) |
| 93 |
(cond ((stream-null? strms) stream-null) |
| 94 |
((not (stream? (stream-car strms))) |
| 95 |
(error 'stream-concat "non-stream object in input stream")) |
| 96 |
((stream-null? (stream-car strms)) |
| 97 |
(stream-concat (stream-cdr strms))) |
| 98 |
(else (stream-cons |
| 99 |
(stream-car (stream-car strms)) |
| 100 |
(stream-concat |
| 101 |
(stream-cons (stream-cdr (stream-car strms)) (stream-cdr strms)))))))) |
| 102 |
(if (not (stream? strms)) |
| 103 |
(error 'stream-concat "non-stream argument") |
| 104 |
(stream-concat strms))) |
| 105 |
|
| 106 |
(define stream-constant |
| 107 |
(stream-lambda objs |
| 108 |
(cond ((null? objs) stream-null) |
| 109 |
((null? (cdr objs)) (stream-cons (car objs) (stream-constant (car objs)))) |
| 110 |
(else (stream-cons (car objs) |
| 111 |
(apply stream-constant (append (cdr objs) (list (car objs))))))))) |
| 112 |
|
| 113 |
(define (stream-drop n strm) |
| 114 |
(define stream-drop |
| 115 |
(stream-lambda (n strm) |
| 116 |
(if (or (zero? n) (stream-null? strm)) |
| 117 |
strm |
| 118 |
(stream-drop (- n 1) (stream-cdr strm))))) |
| 119 |
(cond ((not (integer? n)) (error 'stream-drop "non-integer argument")) |
| 120 |
((negative? n) (error 'stream-drop "negative argument")) |
| 121 |
((not (stream? strm)) (error 'stream-drop "non-stream argument")) |
| 122 |
(else (stream-drop n strm)))) |
| 123 |
|
| 124 |
(define (stream-drop-while pred? strm) |
| 125 |
(define stream-drop-while |
| 126 |
(stream-lambda (strm) |
| 127 |
(if (and (stream-pair? strm) (pred? (stream-car strm))) |
| 128 |
(stream-drop-while (stream-cdr strm)) |
| 129 |
strm))) |
| 130 |
(cond ((not (procedure? pred?)) (error 'stream-drop-while "non-procedural argument")) |
| 131 |
((not (stream? strm)) (error 'stream-drop-while "non-stream argument")) |
| 132 |
(else (stream-drop-while strm)))) |
| 133 |
|
| 134 |
(define (stream-filter pred? strm) |
| 135 |
(define stream-filter |
| 136 |
(stream-lambda (strm) |
| 137 |
(cond ((stream-null? strm) stream-null) |
| 138 |
((pred? (stream-car strm)) |
| 139 |
(stream-cons (stream-car strm) (stream-filter (stream-cdr strm)))) |
| 140 |
(else (stream-filter (stream-cdr strm)))))) |
| 141 |
(cond ((not (procedure? pred?)) (error 'stream-filter "non-procedural argument")) |
| 142 |
((not (stream? strm)) (error 'stream-filter "non-stream argument")) |
| 143 |
(else (stream-filter strm)))) |
| 144 |
|
| 145 |
(define (stream-fold proc base strm) |
| 146 |
(cond ((not (procedure? proc)) (error 'stream-fold "non-procedural argument")) |
| 147 |
((not (stream? strm)) (error 'stream-fold "non-stream argument")) |
| 148 |
(else (let loop ((base base) (strm strm)) |
| 149 |
(if (stream-null? strm) |
| 150 |
base |
| 151 |
(loop (proc base (stream-car strm)) (stream-cdr strm))))))) |
| 152 |
|
| 153 |
(define (stream-for-each proc . strms) |
| 154 |
(define (stream-for-each strms) |
| 155 |
(if (not (exists stream-null? strms)) |
| 156 |
(begin (apply proc (map stream-car strms)) |
| 157 |
(stream-for-each (map stream-cdr strms))))) |
| 158 |
(cond ((not (procedure? proc)) (error 'stream-for-each "non-procedural argument")) |
| 159 |
((null? strms) (error 'stream-for-each "no stream arguments")) |
| 160 |
((exists (lambda (x) (not (stream? x))) strms) |
| 161 |
(error 'stream-for-each "non-stream argument")) |
| 162 |
(else (stream-for-each strms)))) |
| 163 |
|
| 164 |
(define (stream-from first . step) |
| 165 |
(define stream-from |
| 166 |
(stream-lambda (first delta) |
| 167 |
(stream-cons first (stream-from (+ first delta) delta)))) |
| 168 |
(let ((delta (if (null? step) 1 (car step)))) |
| 169 |
(cond ((not (number? first)) (error 'stream-from "non-numeric starting number")) |
| 170 |
((not (number? delta)) (error 'stream-from "non-numeric step size")) |
| 171 |
(else (stream-from first delta))))) |
| 172 |
|
| 173 |
(define (stream-iterate proc base) |
| 174 |
(define stream-iterate |
| 175 |
(stream-lambda (base) |
| 176 |
(stream-cons base (stream-iterate (proc base))))) |
| 177 |
(if (not (procedure? proc)) |
| 178 |
(error 'stream-iterate "non-procedural argument") |
| 179 |
(stream-iterate base))) |
| 180 |
|
| 181 |
(define (stream-length strm) |
| 182 |
(if (not (stream? strm)) |
| 183 |
(error 'stream-length "non-stream argument") |
| 184 |
(let loop ((len 0) (strm strm)) |
| 185 |
(if (stream-null? strm) |
| 186 |
len |
| 187 |
(loop (+ len 1) (stream-cdr strm)))))) |
| 188 |
|
| 189 |
(define-syntax stream-let |
| 190 |
(syntax-rules () |
| 191 |
((stream-let tag ((name val) ...) body1 body2 ...) |
| 192 |
((letrec ((tag (stream-lambda (name ...) body1 body2 ...))) tag) val ...)))) |
| 193 |
|
| 194 |
(define (stream-map proc . strms) |
| 195 |
(define stream-map |
| 196 |
(stream-lambda (strms) |
| 197 |
(if (exists stream-null? strms) |
| 198 |
stream-null |
| 199 |
(stream-cons (apply proc (map stream-car strms)) |
| 200 |
(stream-map (map stream-cdr strms)))))) |
| 201 |
(cond ((not (procedure? proc)) (error 'stream-map "non-procedural argument")) |
| 202 |
((null? strms) (error 'stream-map "no stream arguments")) |
| 203 |
((exists (lambda (x) (not (stream? x))) strms) |
| 204 |
(error 'stream-map "non-stream argument")) |
| 205 |
(else (stream-map strms)))) |
| 206 |
|
| 207 |
(define-syntax stream-match |
| 208 |
(syntax-rules () |
| 209 |
((stream-match strm-expr clause ...) |
| 210 |
(let ((strm strm-expr)) |
| 211 |
(cond |
| 212 |
((not (stream? strm)) (error 'stream-match "non-stream argument")) |
| 213 |
((stream-match-test strm clause) => car) ... |
| 214 |
(else (error 'stream-match "pattern failure"))))))) |
| 215 |
|
| 216 |
(define-syntax stream-match-test |
| 217 |
(syntax-rules () |
| 218 |
((stream-match-test strm (pattern fender expr)) |
| 219 |
(stream-match-pattern strm pattern () (and fender (list expr)))) |
| 220 |
((stream-match-test strm (pattern expr)) |
| 221 |
(stream-match-pattern strm pattern () (list expr))))) |
| 222 |
|
| 223 |
(define-syntax stream-match-pattern |
| 224 |
(lambda (x) |
| 225 |
(define (wildcard? x) |
| 226 |
(and (identifier? x) |
| 227 |
(free-identifier=? x (syntax _)))) |
| 228 |
(syntax-case x () |
| 229 |
((stream-match-pattern strm () (binding ...) body) |
| 230 |
(syntax (and (stream-null? strm) (let (binding ...) body)))) |
| 231 |
((stream-match-pattern strm (w? . rest) (binding ...) body) |
| 232 |
(wildcard? #'w?) |
| 233 |
(syntax (and (stream-pair? strm) |
| 234 |
(let ((strm (stream-cdr strm))) |
| 235 |
(stream-match-pattern strm rest (binding ...) body))))) |
| 236 |
((stream-match-pattern strm (var . rest) (binding ...) body) |
| 237 |
(syntax (and (stream-pair? strm) |
| 238 |
(let ((temp (stream-car strm)) (strm (stream-cdr strm))) |
| 239 |
(stream-match-pattern strm rest ((var temp) binding ...) body))))) |
| 240 |
((stream-match-pattern strm w? (binding ...) body) |
| 241 |
(wildcard? #'w?) |
| 242 |
(syntax (let (binding ...) body))) |
| 243 |
((stream-match-pattern strm var (binding ...) body) |
| 244 |
(syntax (let ((var strm) binding ...) body)))))) |
| 245 |
|
| 246 |
(define-syntax stream-of |
| 247 |
(syntax-rules () |
| 248 |
((_ expr rest ...) |
| 249 |
(stream-of-aux expr stream-null rest ...)))) |
| 250 |
|
| 251 |
(define-syntax stream-of-aux |
| 252 |
(syntax-rules (in is) |
| 253 |
((stream-of-aux expr base) |
| 254 |
(stream-cons expr base)) |
| 255 |
((stream-of-aux expr base (var in stream) rest ...) |
| 256 |
(stream-let loop ((strm stream)) |
| 257 |
(if (stream-null? strm) |
| 258 |
base |
| 259 |
(let ((var (stream-car strm))) |
| 260 |
(stream-of-aux expr (loop (stream-cdr strm)) rest ...))))) |
| 261 |
((stream-of-aux expr base (var is exp) rest ...) |
| 262 |
(let ((var exp)) (stream-of-aux expr base rest ...))) |
| 263 |
((stream-of-aux expr base pred? rest ...) |
| 264 |
(if pred? (stream-of-aux expr base rest ...) base)))) |
| 265 |
|
| 266 |
(define (stream-range first past . step) |
| 267 |
(define stream-range |
| 268 |
(stream-lambda (first past delta lt?) |
| 269 |
(if (lt? first past) |
| 270 |
(stream-cons first (stream-range (+ first delta) past delta lt?)) |
| 271 |
stream-null))) |
| 272 |
(cond ((not (number? first)) (error 'stream-range "non-numeric starting number")) |
| 273 |
((not (number? past)) (error 'stream-range "non-numeric ending number")) |
| 274 |
(else (let ((delta (cond ((pair? step) (car step)) ((< first past) 1) (else -1)))) |
| 275 |
(if (not (number? delta)) |
| 276 |
(error 'stream-range "non-numeric step size") |
| 277 |
(let ((lt? (if (< 0 delta) < >))) |
| 278 |
(stream-range first past delta lt?))))))) |
| 279 |
|
| 280 |
(define (stream-ref strm n) |
| 281 |
(cond ((not (stream? strm)) (error 'stream-ref "non-stream argument")) |
| 282 |
((not (integer? n)) (error 'stream-ref "non-integer argument")) |
| 283 |
((negative? n) (error 'stream-ref "negative argument")) |
| 284 |
(else (let loop ((strm strm) (n n)) |
| 285 |
(cond ((stream-null? strm) (error 'stream-ref "beyond end of stream")) |
| 286 |
((zero? n) (stream-car strm)) |
| 287 |
(else (loop (stream-cdr strm) (- n 1)))))))) |
| 288 |
|
| 289 |
(define (stream-reverse strm) |
| 290 |
(define stream-reverse |
| 291 |
(stream-lambda (strm rev) |
| 292 |
(if (stream-null? strm) |
| 293 |
rev |
| 294 |
(stream-reverse (stream-cdr strm) (stream-cons (stream-car strm) rev))))) |
| 295 |
(if (not (stream? strm)) |
| 296 |
(error 'stream-reverse "non-stream argument") |
| 297 |
(stream-reverse strm stream-null))) |
| 298 |
|
| 299 |
(define (stream-scan proc base strm) |
| 300 |
(define stream-scan |
| 301 |
(stream-lambda (base strm) |
| 302 |
(if (stream-null? strm) |
| 303 |
(stream base) |
| 304 |
(stream-cons base (stream-scan (proc base (stream-car strm)) (stream-cdr strm)))))) |
| 305 |
(cond ((not (procedure? proc)) (error 'stream-scan "non-procedural argument")) |
| 306 |
((not (stream? strm)) (error 'stream-scan "non-stream argument")) |
| 307 |
(else (stream-scan base strm)))) |
| 308 |
|
| 309 |
(define (stream-take n strm) |
| 310 |
(define stream-take |
| 311 |
(stream-lambda (n strm) |
| 312 |
(if (or (stream-null? strm) (zero? n)) |
| 313 |
stream-null |
| 314 |
(stream-cons (stream-car strm) (stream-take (- n 1) (stream-cdr strm)))))) |
| 315 |
(cond ((not (stream? strm)) (error 'stream-take "non-stream argument")) |
| 316 |
((not (integer? n)) (error 'stream-take "non-integer argument")) |
| 317 |
((negative? n) (error 'stream-take "negative argument")) |
| 318 |
(else (stream-take n strm)))) |
| 319 |
|
| 320 |
(define (stream-take-while pred? strm) |
| 321 |
(define stream-take-while |
| 322 |
(stream-lambda (strm) |
| 323 |
(cond ((stream-null? strm) stream-null) |
| 324 |
((pred? (stream-car strm)) |
| 325 |
(stream-cons (stream-car strm) (stream-take-while (stream-cdr strm)))) |
| 326 |
(else stream-null)))) |
| 327 |
(cond ((not (stream? strm)) (error 'stream-take-while "non-stream argument")) |
| 328 |
((not (procedure? pred?)) (error 'stream-take-while "non-procedural argument")) |
| 329 |
(else (stream-take-while strm)))) |
| 330 |
|
| 331 |
(define (stream-unfold mapper pred? generator base) |
| 332 |
(define stream-unfold |
| 333 |
(stream-lambda (base) |
| 334 |
(if (pred? base) |
| 335 |
(stream-cons (mapper base) (stream-unfold (generator base))) |
| 336 |
stream-null))) |
| 337 |
(cond ((not (procedure? mapper)) (error 'stream-unfold "non-procedural mapper")) |
| 338 |
((not (procedure? pred?)) (error 'stream-unfold "non-procedural pred?")) |
| 339 |
((not (procedure? generator)) (error 'stream-unfold "non-procedural generator")) |
| 340 |
(else (stream-unfold base)))) |
| 341 |
|
| 342 |
(define (stream-unfolds gen seed) |
| 343 |
(define (len-values gen seed) |
| 344 |
(call-with-values |
| 345 |
(lambda () (gen seed)) |
| 346 |
(lambda vs (- (length vs) 1)))) |
| 347 |
(define unfold-result-stream |
| 348 |
(stream-lambda (gen seed) |
| 349 |
(call-with-values |
| 350 |
(lambda () (gen seed)) |
| 351 |
(lambda (next . results) |
| 352 |
(stream-cons results (unfold-result-stream gen next)))))) |
| 353 |
(define result-stream->output-stream |
| 354 |
(stream-lambda (result-stream i) |
| 355 |
(let ((result (list-ref (stream-car result-stream) (- i 1)))) |
| 356 |
(cond ((pair? result) |
| 357 |
(stream-cons |
| 358 |
(car result) |
| 359 |
(result-stream->output-stream (stream-cdr result-stream) i))) |
| 360 |
((not result) |
| 361 |
(result-stream->output-stream (stream-cdr result-stream) i)) |
| 362 |
((null? result) stream-null) |
| 363 |
(else (error 'stream-unfolds "can't happen")))))) |
| 364 |
(define (result-stream->output-streams result-stream) |
| 365 |
(let loop ((i (len-values gen seed)) (outputs '())) |
| 366 |
(if (zero? i) |
| 367 |
(apply values outputs) |
| 368 |
(loop (- i 1) (cons (result-stream->output-stream result-stream i) outputs))))) |
| 369 |
(if (not (procedure? gen)) |
| 370 |
(error 'stream-unfolds "non-procedural argument") |
| 371 |
(result-stream->output-streams (unfold-result-stream gen seed)))) |
| 372 |
|
| 373 |
(define (stream-zip . strms) |
| 374 |
(define stream-zip |
| 375 |
(stream-lambda (strms) |
| 376 |
(if (exists stream-null? strms) |
| 377 |
stream-null |
| 378 |
(stream-cons (map stream-car strms) (stream-zip (map stream-cdr strms)))))) |
| 379 |
(cond ((null? strms) (error 'stream-zip "no stream arguments")) |
| 380 |
((exists (lambda (x) (not (stream? x))) strms) |
| 381 |
(error 'stream-zip "non-stream argument")) |
| 382 |
(else (stream-zip strms))))) |