[Haskell-cafe] Library design question

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Thu Sep 18 18:25:16 EDT 2008


On Thu, 2008-09-18 at 15:43 -0300, Andre Nathan wrote:

> My Graph type is the following.
> 
>   data Graph a b = Graph
>     { adjacencies :: Map Int (a, (Map Int b))
>     , numVertices :: Int
>     , numEdges    :: Int
>     }

>   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 }

> So I'm confused right now about how I should proceed. Any hints
> regarding that?

To be honest I would not bother with the state monad and just make them
pure operations returning new graphs:

addVertex :: Int -> a -> Graph a b -> Graph a b

It's very common to have this style, ie returning a new/updated
structure explicitly rather than implicitly in a state monad. Just look
at the Data.Map api for example. If you later want to stick it in a
state monad then that would be straightforward but also easy to use
directly.

Duncan



More information about the Haskell-Cafe mailing list