# Streams: exam-level questions

If you need help reviewing Streams, take a look at these resources:

Each question has a "Toggle Solution" button -- click it to reveal that question's solution.

## Conceptual Questions

### Question 1

Given the following function, list the first 5 elements of the Stream that is returned by `stream1()`.

``````(define (stream1)
(cons-stream 0
(cons-stream 1
(add-streams (stream1) (stream-cdr (stream1))))))``````

0, 1, 1, 2, 3 (the Fibonacci numbers)

### Question 2

Given the following function, list the first 5 elements of the Stream that is returned by `stream2()`.

``````(define (stream2)
(cons-stream 1

1, 2, 4, 8, 16 (the powers of 2)

## Code-Writing questions

### Question 3

Create a function `make-fact-stream`, which returns a Stream whose nth element is n! (factorial of n).

``````(define (make-fact-stream)
'YOUR-CODE-HERE
)

scm> (stream-to-list (make-fact-stream) 4)
(1 1 2 6)``````

Hint: Try writing an iterative factorial function first, then convert it into the Stream function. You should also use a helper function.

``````(define (make-fact-stream)
(helper 0 1))

(define (helper n total)
(cons-stream total
(helper (+ n 1)
(* total (+ n 1)))))

# here's the iterative factorial we use to convert
def infinite_fact():
n, total = 0, 1
while True:
print(total)
n, total = n + 1, total * (n + 1)``````

## More practice problems

Note: The following link contains Stream questions, but the questions use the Python version of streams. The thought process behind each problem is the same, but if you feel that you would not benefit from reading Python stream code (as opposed to Scheme stream code), then it's better if you find stream questions elsewhere (Summer 2014 final is a good idea).

This link contains Stream problems (as well as iterator and generator problems) from the summer 2012 version of 61A. They are pretty tough (probably tougher than final material), so don't get discouraged if you get stuck!

Solutions are also included in the lab (in the form of toggle buttons). Don't worry about the Py section — we didn't cover that this semester.