[Haskell-cafe] Library design question

Henning Thielemann lemming at henning-thielemann.de
Thu Sep 18 15:15:07 EDT 2008


On Thu, 18 Sep 2008, Andre Nathan wrote:

> The issue I hit was when writing the function to add a vertex to the
> graph. Since I need to update the vertex counter, I've used the state
> monad:
>
>  addVertex :: Int -> a -> State (Graph a b) ()
>  addVertex vertex label = do
>    g <- get
>    let adj = Map.insert vertex (label, Map.empty) (adjacencies g)
>    put $ g { adjacencies = adj, numVertices = numVertices g + 1 }
>
> That works fine, but from the point of view of a user of the library,
> the addVertex function should take a "Graph a b" as its first argument,
> as one would expect to be able to say which graph is going to be
> modified.

Think of the state monad as processing a graph in-place. Which graph is 
addressed is declared when running the State monad using runState or 
evalState.


More information about the Haskell-Cafe mailing list