home *** CD-ROM | disk | FTP | other *** search
- ; Die Funktion g verwandelt eine normale Funktion in eine,
- ; die Streams als Argumente nimmt, und den Stream der
- ; Resultate zurückgibt. Bsp.:
- ; (define a (cons-stream 1 (cons-stream 2 the-empty-stream)))
- ; (define b (cons-stream 100 (cons-stream 101 the-empty-stream)))
- ; (define s ((g +) a b))
- ; (head s) ===> 101
- ; (head (tail s)) ===> 102
- ; (head (tail (tail s))) ===> 102
- ; (head (tail (tail (tail s)))) ===> 103
- ; (define s1 ((g +) 1 b))
- ; (head s1) ===> 101
- ; (head (tail s1)) ===> 102
-
- (define (g proc)
- (lambda ops
- (let ((args (map (lambda (x)
- (if (stream? x) x
- (cons-stream x the-empty-stream)))
- ops)))
- (if (= 1 (length args))
- (map-stream proc args)
- (process proc args)))))
-
- (define (process proc args)
- (let ((l (length args)))
- (cond ((= 2 l) (p2 proc (car args) (cadr args)))
- (else (error "Too many arguments")))))
-
- (define (p2 proc a b)
- (cond ((empty-stream? a) the-empty-stream)
- ((empty-stream? b) the-empty-stream)
- (else (flatten (map-stream (lambda (a1)
- (map-stream (lambda (x) (proc a1 x))
- b))
- a)))))
-
- (define (map-stream proc s)
- (if (empty-stream? s) the-empty-stream
- (cons-stream (proc (head s))
- (map-stream proc (tail s)))))
-
- (define (append-delayed s ds)
- (if (empty-stream? s) (force ds)
- (cons-stream (head s)
- (append-delayed (tail s) ds))))
-
- (define (flatten stream)
- (if (empty-stream? stream) the-empty-stream
- (append-delayed (head stream) (delay (flatten (tail stream))))))
-
- (define (error msg)
- (print "too many arguments to generator")
- nil)