| C511-001/002 | Organization of Programming Languages | Winter 1999 |
|---|---|---|
Homework Assignment Number 3 |
Due: February 5, 1999 (Soft) March 2 (Hard - NEW - NEW - NEW!!!!!!)
Send Result (Scheme code) to: hchen@boole.ececs.uc.edu
Repeat assignment number two using the Scheme programming language. That is, develop a general purpose facility in Scheme that allows consumers and producers to communicate (and transfer tokens). Your facility should have some means to establish stream connections, some means to "put" tokens into the stream and some means to "get" tokens from the stream.
I built such a facility whose use is illustrated in the following example which connects a fibonacci number producer:
(define fibonacci
(lambda (f-cur)
(let ((fib (connect! f-cur)))
(letrec ((f (lambda (s1 s2)
(put! (+ (car-s s1) (car-s s2)) f-cur)
(f (cdr-s s1) (cdr-s s2)))))
(put! 1 f-cur)
(put! 1 f-cur)
(f fib (cdr-s fib))))))
to a test consumer which requests the first n fibonacci numbers:
(define test-fib
(lambda (n)
(letrec
((alpha (lambda (x s)
(if (= x 0)
'()
(cons (car-s s) (alpha (- x 1) (cdr-s s)))))))
(alpha n (open! channel (lambda () (fibonacci channel)))))))
where stream channel is initialized with:
(define channel (init-stream))
and (cdr-s s) evaluates to a stream equal to stream s except for the first token and (car-s s) evaluates to the first token of stream s. I developed connect!, open!, put!, car-s, cdr-s, init-stream to make it work. Please come up with a workable solution that makes sense to you.