12.1 Generating Sequences
There are several ways to build a sequence, first look at our choice, the direct way is to implement the IEnumerator <T> interface, provide the current property, and the MoveNext method, move the enumerator object to the next element. This requires explicitly creating an object with mutable state, which is clearly a violation of the functional style. The usual practice is to hide the variability, providing a more declarative method of expressing the contents of the generated sequence, similar to the delay value used in the previous chapter. The explicit use of mutable states (for example, implementing caching) does not seem like a good functional style, but when we hide the variability in the lazy< ' t> type, we get a perfectly reasonable functional code.
In general, in functional programming, we can use higher-order functions. The F # Library provides a number of function processing sequences, but we only look at one example. As we'll see later, C # and F # have simple ways to generate sequences, and in C # we can use iterations where F # provides a general purpose sequence processing feature called a sequence expression (sequence expressions).
12.1 Generating Sequences