Problem 5: My cons-stream
(optional, 0pts)
Define my-cons-stream
, which returns a pair of elements, where the second element is not evaluated until my-cdr-stream
is called on it. Also define the procedure my-car
and my-cdr-stream
, which take in a stream returned by my-cons-stream
and return the first element and the result of evaluating the second element in the stream pair, respectively.
Unlike the streams we’ve seen in lecture, if you repeatedly call my-cdr-stream
on a stream returned by my-cons-stream
, you may evaluate an expression multiple times.
(define (my-cons-stream first second) ; Does this line need to be changed?
'YOUR-CODE-HERE
)
(define (my-car stream)
'YOUR-CODE-HERE
)
(define (my-cdr-stream stream)
'YOUR-CODE-HERE
)
;;; Tests
; scm> (define a (my-cons-stream 1 (my-cons-stream (print 2) nil)))
; a
; scm> (my-car a)
; 1
; scm> (define b (my-cdr-stream a))
; 2
; b
; scm> (define c (my-cdr-stream a))
; 2
; c
; scm> (my-cdr-stream b)
; ()