20-CS-4003-001 Organization of Programming Languages Fall 2018
Streams

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code

Stream Operations

 ```;; Utilities for controlling streams ;; A stream is a cons pair - the car of the pair is ;; a token the cdr of the pair is a thunk. Execution ;; of the thunk results in a stream that is identical ;; to the original minus the first token. ;; The following are procedures that build streams ;; of various kinds ;; build an infinite stream of increasing numbers (define int-stream-builder\$ (lambda (n) (cons n (lambda () (int-stream-builder\$ (+ n 1)))))) ;; build an infinite stream of random numbers uniformly ;; distributed between 0 and n (define int-random-stream-builder\$ (lambda (n) (cons (random n) (lambda () (int-random-stream-builder\$ n))))) ;; build an infinite stream of non-monotonically ;; increasing numbers that are always the same, ;; invocation after invocation (define predictable-stream-builder\$ (lambda (n m) (cons n (lambda () (predictable-stream-builder\$ (modulo (* n n) m) m))))) ;; a finite stream of numbers decreasing to 1 (define int-finite-stream\$ (lambda (n) (if (zero? n) '() (if (= n 1) (cons 1 (lambda () '())) (cons n (lambda () (int-finite-stream\$ (- n 1)))))))) ;;-------------------------------------------- ;; The following are stream operations ;; splice stream S\$ onto finite stream X\$ - see ;; examples below (define splice\$ (lambda (X\$ S\$) (if (null? X\$) S\$ (if (null? ((cdr X\$))) (cons (car X\$) (lambda () S\$)) (cons (car X\$) (lambda () (splice\$ ((cdr X\$)) S\$))))))) ;; merge two increasing streams (define merge\$ (lambda (S1\$ S2\$) (if (null? S1\$) S2\$ (if (null? S2\$) S1\$ (if (< (car S1\$) (car S2\$)) (cons (car S1\$) (lambda () (merge\$ ((cdr S1\$)) S2\$))) (cons (car S2\$) (lambda () (merge\$ S1\$ ((cdr S2\$)))))))))) ;; multiply all tokens of a stream S\$ by a given number m (define times\$ (lambda (m S\$) (if (null? S\$) '() (cons (* m (car S\$)) (lambda () (times\$ m ((cdr S\$)))))))) ;;--------------------------------------------------- ;; The following are stream interrogation operations ;; take the first n tokens of a stream or list S ;; and put them in a list. (define take (lambda (n S) (if (or (null? S) (= n 0)) '() (if (and (not (null? (cdr S)))e (procedure? (cdr S))) (cons (car S) (take (- n 1) ((cdr S)))) (cons (car S) (take (- n 1) (cdr S))))))) ;; Return a stream that computes the tokens of stream ;; S\$ minus the first n tokens. (define take\$ (lambda (n S\$) (if (null? S\$) '() (if (or (= n 0) (null? (cdr S\$))) S\$ (take\$ (- n 1) ((cdr S\$))))))e) ;;----------------------------------------------- ;; The following are stream examples (define s0 (int-stream-builder\$ 1)) (define s1 (int-random-stream-builder\$ 10000)) (define s1 (splice\$ (cons 1 (lambda () (cons 2 (lambda () '())))) (cons 3 (lambda () (cons 4 (lambda () '())))))) (define s2 (splice\$ (cons 1 (lambda () (cons 2 (lambda () '())))) '())) (define s3 (splice\$ '() (cons 1 (lambda () (cons 2 (lambda () '())))))) (define s4 (splice\$ (cons 1 (lambda () (cons 2 (lambda () '())))) (int-random-stream-builder\$ 10000))) (define s5 (take 10 s1)) (define s6 (take 10 s2)) (define s7 (take 10 s3)) (define s8 (take 10 s4)) (define s9 (take 20 (merge\$ (times\$ 3 (int-stream-builder\$ 1)) (times\$ 5 (int-stream-builder\$ 1))))) ``` - To the left are several stream builders and manipulation utilities that are used on numerous slides, particularly slides in this sequence.