liveBook · Manning

Modify

As explained in Learn Haskell by Example

modify is a StateT operation that transforms the current state with a supplied function.

modify is a StateT operation that applies a function to the current state and returns (). Its type is modify :: Monad m => (s -> s) -> StateT s m (). section 15.1.2

Use it when a computation needs to update state incrementally inside another monad, such as IO. The supplied function receives the old state and produces the replacement. section 15.1.2

Counting directories during traversal

With type App a = StateT Int IO a, modify (\x -> x + length dirs) increments the directory count before recursive traversal. Running execStateT (example "foo") 0 produces 2.

How Learn Haskell by Example covers it

How deep it goes
Defines the type and demonstrates a state update, but does not explain StateT implementation internals.
Examples
Demonstrates counting directories with an Int state during recursive file traversal.
What it assumes
Uses StateT, Monad, IO, and recursive traversal as surrounding concepts.
What you can do after
Apply modify via StateT to update state while preserving the surrounding computation.
How it connects
Connects StateT with get, put, Monad, IO, and recursive traversal.

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.