Velyyn S
Velyyn S

Reputation: 89

Extracting elements of a graph in Haskell

I have a little question concerning Haskell. If I have data type representing a graph like this one :

import Data.Map (Map,empty,member,insert)

import Graphviz

-- | A directed graph
    data Graph v = Graph
    { arcsMap :: Map v [v]     -- A map associating a vertex with its 
                                  successors
    , labelMap :: Map v String -- The Graphviz label of each node
    , styleMap :: Map v String -- The Graphviz style of each node
    }

And i want to create a list by accessing every vertices of a given graph.

Like this:

-- | Returns the list of vertices of a graph in ascending order
--
-- >>> vertices emptyGraph
-- []
-- >>> vertices $ addVertices emptyGraph [1,4,5,2,1]
-- [1,2,4,5]
vertices :: Graph v -> [v]

My question is how can I tell Haskell to look into every vertices in arcsMap and create a list with it ? Thankyou !!!

Upvotes: 0

Views: 325

Answers (1)

Garrison
Garrison

Reputation: 386

The function keys returns all keys of the map. So you could implement vertices like this:

vertices :: Graph v -> [v]
vertices = keys . arcsMap

Upvotes: 2

Related Questions