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

Lambda calculus, Type theory, Formal semantics, Program analysis

All lectures
Hamming's Sequence

Compare threaded streams and non-blocking streams

 Stream.java Hamming_0.java - Uses threaded monitors StreamC.java Hamming_1.java - Uses non-blocking streams

Problem:
Given: Set P of distinct primes.
Produce: Distinct set of integers whose prime factors are in P.

Example: For P = {3,5,11}

Solution: 3,5,9,11,15,25,27,33,45,55,75,81,99,121,125,135 ...

Possible Program

```   For all numbers 1,2,3 ... do the following:
If the number has only prime factors in P, print it.
```

This is unfortunately too slow to be of value.

Better Program

```        _  5, 11, 25, 55, 121, 125, ...
/
/
3 <
\
\_  9, 15, 27, 33, 45, 75, 81, 99, 135, ...
3 * {3, 5, 9, 11, 15, 25, 27, 33, 45, ...}
```
So, generalizing:
```   Hamming(P) =
if  P is empty then return nothing.
return
First(P)
+
Merge(First(P)*Hamming(P), Hamming(Rest(P))).
```