I just started Unix System Programming with Standard ML and starting on page 22 Shipman begins to explain a pure functional way of avoiding the constant state changes of typing at a keyboard:
A lazy stream is an infinite list of values that is computed lazily. The stream of keystrokes that a user presses on the keyboard can be represented as an infinite (or arbitrarily long) list of all of the keystrokes that the user is ever going to press. You get the next keystroke by taking the head element of the stream and saving the remainder. Since the head is only computed lazily, on demand, this will result in a call to the operating system to get the next keystroke. What’s special about the lazy stream approach is that you can treat the entire stream as a value and pass it around in your program. You can pass it as an argument to a function or save the stream in a data structure. You can write functions that operate on the stream as a whole. You can write a word scanner as a function,
toWords
, that transforms a stream of characters into a stream of words. A program to obtain the next 100 words from the standard input might look something likeapply show (take 100 (toWords stdIn))
wherestdIn
is the input stream andtake n
is a function that returns the firstn
elements in a list. Theshow
function is applied to each word to print it out. The program is just the simple composition of the functions. Lazy evaluation ensures that this program is equivalent to the set of nested loops that you would write for this in an [imperative] program. This is programming at a much higher level than you typically get with [imperative] languages.
I take it he doesn't mean just a list of the 26 letters of the alphabet, rather, something more like every possible word and combination thereof, i.e., similar to the theoretical possibility of finding yours and everyone's birthday if you look hard enough in the irrational never-repeating infinite stream of $\pi$ places. That is, somewhere in the infinite series of $\pi$ decimal places is your birthday mmddyyyy-style. Is this in fact what is meant here? Also, is this in any way related to infinite recursion?
In a similar vein, I once was told that no functional language is truly pure, and he gave the example of a function that "goes out" and gets the exact time from an atomic clock. Is this the same sort of issue, where, e.g., an infinite list of possible times is available for a list operation?