Streams Practice Problems

Important! These are all exam-level problems. Do not attempt these problems without a solid foundation in the subject and use them for exam practice. Moreover, for this section, we will be using the scheme version of streams taught in Fall 2015.

1. Answer the following questions:
    a. Why do we use streams?
    b. How large can a stream be?
    c. When does a value of a stream get computed?

a. Lazy Computation
b. Infinitely large
c. Once that specific section of the stream is called.  
"YOUR ANSWERS HERE"
Toggle Solution

2. I have a scheme list of items, each of which is a scheme list of length two, the first element being the name of the number, the second is the string “naughty” or “nice”. Help implement Santas Nice-Or-Naughty stream, which is just a stream of name of the nice numbers. Because Santa is infinitely giving, the stream should be infinite and loop around. Assume the list is non-empty and contains atleast one nice element because not everyone is evil.

(define (norn lst)
    (if (= (car (cdr (car lst))) ‘nice) (cons-stream (car (car lst)) 
    (norn (append (cdr lst) (list (car lst)))) (norn (cdr lst)) 
(define (norn lst)
"""
(car lst) is a scheme lst of two items, the first being a number and the second being nice or naughty, e.g: ('nine', 'naughty'). 
(norn '(('nine', 'nice'), ('seven', 'naughty'))) should return a Stream with 'nine' in it forever. 
"""
    
Toggle Solution