;;;; streams-utils.incl.scm ;;;; Kon Lovett, Feb '19 ;;;; Kon Lovett, Apr '09 ; Copyright (C) 2007 by Philip L. Bewig of Saint Louis, Missouri, USA. All rights ; reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of ; this software and associated documentation files (the "Software"), to deal in the Software ; without restriction, including without limitation the rights to use, copy, modify, merge, ; publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to ; whom the Software is furnished to do so, subject to the following conditions: The above ; copyright notice and this permission notice shall be included in all copies or substantial ; portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS ; FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF ; CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR ; THE USE OR OTHER DEALINGS IN THE SOFTWARE. ;;; (define-inline (%check-streams loc strms #!optional argnam) (when (null? strms) (error loc "no stream arguments" strms)) (for-each (cut check-stream loc <> argnam) strms) strms ) ;(append xs args) = (reverse (append (reverse args) (reverse xs))) (define (right-section fn . args) (lambda xs (apply fn (append xs args)))) ;;; (define-stream (stream-intersperse yy x) (stream-match (check-stream 'stream-intersperse yy 'stream) (() (stream (stream x)) ) ((y . ys) (stream-append (stream (stream-cons x yy)) (stream-map (lambda (z) (stream-cons y z)) (stream-intersperse ys x))) ) ) ) (define-stream (stream-permutations xs) (if (stream-null? (check-stream 'stream-permutations xs 'stream)) (stream (stream)) (stream-concat (stream-map (right-section stream-intersperse (stream-car xs)) (stream-permutations (stream-cdr xs)))) ) ) (define-stream (file->stream filename #!optional (reader read-char)) (check-procedure 'file->streams reader 'reader) (let ((port (open-input-file (check-string 'file->streams filename 'filename)))) (stream-let loop ((item (reader port))) (if (eof-object? item) (begin (close-input-port port) stream-null) (stream-cons item (loop (reader port))) ) ) ) ) (define (stream-split count strm) (check-stream 'stream-split strm 'stream) (check-natural-integer 'stream-split count 'count) (values (stream-take count strm) (stream-drop count strm))) (define-stream (stream-unique eql? strm) (check-procedure 'stream-unique eql? 'equivalence) (stream-let loop ((strm (check-stream 'stream-unique strm 'stream))) (if (stream-null? strm) stream-null (stream-cons (stream-car strm) (loop (stream-drop-while (lambda (x) (eql? (stream-car strm) x)) strm))) ) ) ) (define (stream-fold-one func strm) (check-stream 'stream-fold-one strm 'stream) (stream-fold (check-procedure 'stream-fold-one func 'function) (stream-car strm) (stream-cdr strm)) ) (define-stream (stream-member eql? item strm) (check-procedure 'stream-member eql? 'equivalence) (stream-let loop ((strm (check-stream 'stream-member strm 'stream))) (cond ((stream-null? strm) #f) ((eql? item (stream-car strm)) strm) (else (loop (stream-cdr strm)) ) ) ) ) (define-stream (stream-merge lt? . strms) ; (define-stream (stream-merge$ xx yy) (stream-match xx (() yy ) ((x . xs) (stream-match yy (() xx ) ((y . ys) (if (lt? y x) (stream-cons y (stream-merge$ xx ys)) (stream-cons x (stream-merge$ xs yy))))) ) ) ) ; (check-procedure 'stream-merge lt? 'less-than) (stream-let loop ((strms (%check-streams 'stream-merge strms 'stream))) (cond ((null? strms) stream-null ) ((null? (cdr strms)) (car strms) ) (else (stream-merge$ (car strms) (apply stream-merge lt? (cdr strms))) ) ) ) ) (define (stream-partition pred? strm) (check-procedure 'stream-partition pred? 'predicate) (stream-unfolds (lambda (s) (if (stream-null? s) (values s '() '()) (let ((a (stream-car s)) (d (stream-cdr s))) (if (pred? a) (values d (list a) #f) (values d #f (list a)) ) ) ) ) (check-stream 'stream-partition strm 'stream)) ) (define-stream (stream-finds eql? item strm) (check-procedure 'stream-finds eql? 'equivalence) (stream-of (car x) (x in (stream-zip (stream-from 0) (check-stream 'stream-finds strm 'stream))) (eql? item (cadr x))) ) (define (stream-find eql? item strm) (check-stream 'stream-find strm 'stream) (check-procedure 'stream-find eql? 'equivalence) (stream-car (stream-append (stream-finds eql? item strm) (stream #f))) ) (define-stream (stream-remove pred? strm) (check-procedure 'stream-remove pred? 'predicate) (stream-filter (complement pred?) (check-stream 'stream-remove strm 'stream)) ) (define (stream-every pred? strm) (check-procedure 'stream-every pred? 'predicate) (let loop ((strm (check-stream 'stream-every strm 'stream))) (cond ((stream-null? strm) #t ) ((not (pred? (stream-car strm))) #f ) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-any pred? strm) (check-procedure 'stream-any pred? 'predicate) (let loop ((strm (check-stream 'stream-any strm 'stream))) (cond ((stream-null? strm) #f ) ((pred? (stream-car strm)) #t ) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-and strm) (let loop ((strm (check-stream 'stream-and strm 'stream))) (cond ((stream-null? strm) #t ) ((not (stream-car strm)) #f ) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-or strm) (check-stream 'stream-or strm 'stream) (let loop ((strm strm)) (cond ((stream-null? strm) #f ) ((stream-car strm) #t ) (else (loop (stream-cdr strm)) ) ) ) ) (define (stream-fold-right func base strm) (check-procedure 'stream-fold-right func 'function) (let loop ((strm (check-stream 'stream-fold-right strm 'stream))) (if (stream-null? strm) base (func (stream-car strm) (loop (stream-cdr strm))) ) ) ) (define (stream-fold-right-one func strm) (check-procedure 'stream-fold-right-one func 'function) (let loop ((strm (check-stream 'stream-fold-right-one strm 'stream))) (stream-match strm ((x) x ) ((x . xs) (func x (loop xs)) ) ) ) ) (define (stream-assoc key dict #!optional (eql? equal?)) (check-procedure 'stream-assoc eql? 'equivalence) (let loop ((dict (check-stream 'stream-assoc dict 'stream))) (cond ((stream-null? dict) #f ) ((eql? key (car (stream-car dict))) (stream-car dict) ) (else (loop (stream-cdr dict)) ) ) ) ) ; May never return (define (stream-equal? eql? xs ys) (let loop ((xs (check-stream 'stream-equal? xs 'stream1)) (ys (check-stream 'stream-equal? ys 'stream2))) (cond ((and (stream-null? xs) (stream-null? ys)) #t ) ((or (stream-null? xs) (stream-null? ys)) #f ) ((not (eql? (stream-car xs) (stream-car ys))) #f ) (else (loop (stream-cdr xs) (stream-cdr ys)) ) ) ) ) (define-stream (stream-quick-sort lt? strm) (check-procedure 'stream-quick-sort lt? 'less-than) (let loop ((strm (check-stream 'stream-quick-sort strm 'stream))) (if (stream-null? strm) stream-null (let ((x (stream-car strm)) (xs (stream-cdr strm))) (stream-append (loop (stream-filter (lambda (u) (lt? u x)) xs)) (stream x) (loop (stream-filter (lambda (u) (not (lt? u x))) xs))) ) ) ) ) (define-stream (stream-insertion-sort lt? strm) ; (define-stream (insert$ strm x) (stream-match strm (() (stream x) ) ((y . ys) (if (lt? y x) (stream-cons y (insert$ ys x)) (stream-cons x strm) ) ) ) ) ; (check-procedure 'stream-insertion-sort lt? 'less-than) (stream-fold insert$ stream-null (check-stream 'stream-insertion-sort strm 'stream)) ) (define-stream (stream-merge-sort lt? strm) (check-procedure 'stream-merge-sort lt? 'less-than) (let loop ((strm (check-stream 'stream-merge-sort strm 'stream))) (let ((n (quotient (stream-length strm) 2))) (if (zero? n) strm (stream-merge lt? (loop (stream-take n strm)) (loop (stream-drop n strm))) ) ) ) ) (define (stream-maximum lt? strm) (check-procedure 'stream-maximum lt? 'less-than) (stream-fold-one (lambda (x y) (if (lt? x y) y x)) (check-stream 'stream-maximum strm 'stream)) ) (define (stream-minimum lt? strm) (check-procedure 'stream-minimum lt? 'less-than) (stream-fold-one (lambda (x y) (if (lt? x y) x y)) (check-stream 'stream-minimum strm 'stream)) ) ;; Lazy binary-tree "same fringe" (define (binary-tree-same-fringe? tree1 tree2 #!optional (eql? equal?)) ; (define-stream (flatten tree) (cond ((null? tree) stream-null ) ((pair? (car tree) ) (stream-append (flatten (car tree)) (flatten (cdr tree)))) (else (stream-cons (car tree) (flatten (cdr tree))) ) ) ) ; (let loop ((t1 (flatten (check-list 'same-fringe? tree1 'tree1))) (t2 (flatten (check-list 'same-fringe? tree2 'tree2)))) (cond ((and (stream-null? t1) (stream-null? t2)) #t ) ((or (stream-null? t1) (stream-null? t2)) #f ) ((not (eql? (stream-car t1) (stream-car t2))) #f ) (else (loop (stream-cdr t1) (stream-cdr t2)) ) ) ) )