Velyyn S
Velyyn S

Reputation: 89

How to convert something to String in Haskell inside a function

I'm really a noob in Haskell and I'm stuck in a stupid function for days now. So I have a homework and the teacher wants us to only modify the functions of this instance of a graphviz module. I tried to implement the first function but I'm unable to convert the id to a string (you will understand why by seeing the graphviz module)

-- | Converts a graph to Graphviz
--
-- >>> putStrLn $ graphvizString $ addArcs emptyGraph [(1,2),(2,3),(1,3)]
-- digraph {
--     1 [label="1"];
--     2 [label="2"];
--     3 [label="3"];
--     1 -> 2;
--     1 -> 3;
--     2 -> 3;
-- }
-- ...
instance (Show v, Ord v) => Graphviz (Graph v) where
    graphvizNodesList (Graph arcs labels styles) = [(id,label,style) | id<-keys, label<-(elems labels), style<-(elems styles)]
    graphvizArcsList g = 

Here is the context because the homework is kinda big so sorry if there is a lot of code.. It's only for context. My problem is only up there in the instance. So this is my graph (in a module called Graph.hs) :

import Data.Map (Map,empty,member,insert,keys,findWithDefault,assocs ,fromList,(!),union,mapWithKey,toList,elems)
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
    }
...
Some functions implemented here not important
...

And this is the graphviz module (Graphviz.hs)

...
blabla
...
-- | A triple @(id,label,style)@ where
--
-- * @id@ is the key identifying the node (must be unique)
-- * @label@ is any string compatible with Graphviz
-- * @style@ is any string describing the Graphviz style of the node
type GraphvizNode = (String, String, String)

-- | A triple @(id1,id2,label)@ where
--
-- * @id1@ is the key of the source vertex
-- * @id2@ is the key of the target vertex
-- * @label@ is the label of the arc
type GraphvizArc = (String, String, String)

-- | Class of types that can be converted to Graphviz strings
class Graphviz g where

    -- | The list of Graphviz nodes
    --
    -- It returns a list of all nodes of the graph, indicating the label and
    -- the style of each node.
    graphvizNodesList :: g -> [GraphvizNode]

    -- | The list of Graphviz arcs
    --
    -- It returns a list of all arcs of the graph, indicating the label of the
    -- arc.
    graphvizArcsList :: g -> [GraphvizArc]

    -- | The header of the Graphviz string
    --
    -- This is the string printed before writing the nodes and the arcs.
    graphvizHeader :: g -> String
    graphvizHeader _ = "digraph {\n"

blablablabla...

Again, sorry for all this code I think its necessary to understand what I dont understand.

My question is simple: how can I implement graphvizNodesList since id is not a string??? How can I convert it so it can fit in my graphviz function.

Thanks a lot and sorry

Upvotes: 1

Views: 176

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83527

graphvizNodesList (Graph arcs labels styles) = [(id,label,style) | id<-keys, label<-(elems labels), style<-(elems styles)]

keys here has type Map k a -> [k] (i.e. its a function) but in this context, it is expected to be type [a] (i.e. a list). You need to apply the keys function to a Map in order to get the keys from that map.

Upvotes: 1

Related Questions