Summary

Integer stream

js
const integers = pair(1, () => stream_map(x => x+1, integers); // delayed by 1

Fibonacci stream

js
const fib = pair(0, () => pair(1, () => add_stream(stream_tail(fib), fib))); // delayed by 2

Alternating stream

js
const alt = pair(1, () => stream_map(x => -x, alt)); // delayed by 1

Indexing

js
stream_ref(s, n) => elem at index n, processes n+1 elems
eval_stream => list of index 0 to n: length = n

Concept

A stream is either:

  • null
  • or a pair whose tail is a nullary function that returns a stream

Lazy evaluation -> next element is only evaluated when needed/called upon

Application

Alternate integers

js
const psum = s => pair(head(s), () => add_stream(stream_tail(s), psum(s))); // delayed by 1 recursively

const integers = psum(ones);

Creates addition operations

Zip streams

js
const zip = (s1, s2) => pair(head(s1), () => zip(s2, stream_tail(s1))); // alternate between the streams

const out = zip(alt, integers);