Imperative programming expresses computation as an ordered sequence of statements that may modify program state. Functional programming is not its opposite: State actions can read and return state without side effects, making the resulting programs referentially transparent and amenable to equational reasoning. section 6.6
State actions preserve imperative sequencing by passing each result into the next computation. Operations such as map and flatMap propagate state explicitly, while a for-comprehension restores a readable imperative mood without changing the underlying program. section 6.6
Sequencing random state
A random computation can obtain x, obtain the next y, generate a list of length x, and return elements reduced modulo y. The for-comprehension exposes those steps as named bindings while retaining a pure State computation. section 6.6
val ns: Rand[List[Int]] =
for
x <- int #1
y <- int #2
xs <- ints(x) #3
yield xs.map(_ % y) #4
Shows imperative-looking sequencing of State actions: retrieve two integers, generate a list, then transform its elements. section 6.6













