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 like apply show (take 100 (toWords stdIn)) where stdIn is the input stream and take n is a function that returns the first n elements in a list. The show 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?


1 Answer 1


No, that's not what the author means. The author means the specific sequence of characters the user will actually type. If the user is about to write a letter to Al that might be:

'D'::'e'::'a'::'r'::' '::'A'::'l'::rest

The point is that you conceptually have "all" of the characters, and you can (with a bit of care) process this list like any other list.

  • $\begingroup$ Thank you, but then what has this approach won in terms of functionally pure versus imperative. I assume this approach keeps state from changing. Can anyone direct me to a tutorial on this subject? $\endgroup$
    – 147pm
    May 24, 2019 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.