Removing String Double Quotes in Haskell

This function generates simple .dot files for visualizing the transition functions of automata using Graphviz. The main goal is to debug large sets of automatically generated transitions (for example, inflections of Latin verbs).

prepGraph :: ( ... ) => NFA c b a -> [String]
prepGraph nfa = "digraph finite_state_machine {"
              : wrapSp "rankdir = LR"
              : wrapSp ("node [shape = circle]" ++ (mapSp (states nfa \\ terminal nfa)))
              : wrapSp ("node [shape = doublecircle]" ++ (mapSp $ terminal nfa))
              : formatGraph nfa ++ ["}"]

formatGraph :: ( ... ) => NFA c b a -> [String]
formatGraph = map formatDelta . deltaTuples
 where formatDelta (a, a', bc) = wrapSp (mkArrow a a' ++ " " ++ mkLabel bc)
       mkArrow x y   = show x ++ " -> " ++ show y
       mkLabel (y, z) = case z of
         (Just t) -> "[ label = \"(" ++ show y ++ ", " ++ show t ++ ")\" ]"
         Nothing  -> "[ label = \"(" ++ show y ++ ", " ++ "Null" ++ ")\" ]"

where wrap, wrapSpand mapSpare formatting functions, like and deltaTuples.

The problem is that it formatGraphkeeps double quotes around the lines, which causes errors in Graphviz. For example, when I print unlines $ prepGraphto a file, I get things like:

0 -> 1 [ label = "('a', "N. SF")" ];

instead

0 -> 1 [ label = "('a', N. SF)" ];

( , "Null", , ). , , "N. SF" , , . , Haskell: show String, ?

+3
4

, Data.Graph.Inductive.Graphviz:

http://hackage.haskell.org/packages/archive/fgl/5.4.2.3/doc/html/src/Data-Graph-Inductive-Graphviz.html

, , - "sq" :

sq :: String -> String
sq s@[c]                     = s
sq ('"':s)  | last s == '"'  = init s
            | otherwise      = s
sq ('\'':s) | last s == '\'' = init s
            | otherwise      = s
sq s                         = s

(, )

+6

- , .

+2

Class :

class GShow a where
   gShow :: a -> String
   gShow = show

instance GShow String where
   show = id

instance GShow Integer
instance GShow Char
-- And so on for all the types you need.

"gShow" - "", "where" . , .

. ( ), , "gShow" :

instance (Show a) => GShow a

, . , - , .

+1

Seems a little ugly, but you can apply filtertoshow t

filter (/='"') (show t)
-1
source

Source: https://habr.com/ru/post/1765399/


All Articles