Haskell中使用Data.Map的Where子句

Haskell中使用Data.Map的Where子句,haskell,graph,functional-programming,Haskell,Graph,Functional Programming,我在哈斯克尔的一个特别的塞纳里奥有一个小问题 我的图表: import Data.Map (Map,empty,member,insert,keys,findWithDefault,assocs ,fromList,(!),union,mapWithKey,adjust) import Graphviz -- | A directed graph data Graph v = Graph { arcsMap :: Map v [v] -- A map associating a

我在哈斯克尔的一个特别的塞纳里奥有一个小问题

我的图表:

import Data.Map (Map,empty,member,insert,keys,findWithDefault,assocs ,fromList,(!),union,mapWithKey,adjust)
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
    }
我的职能:

-- | Replaces the labels of each vertex by an empty string
--
-- >>> g = addArcs emptyGraph [(1,2),(2,3),(1,3)]
-- >>> putStrLn $ graphvizString $ unlabelGraph g
-- digraph {
--     1 [label=""];
--     2 [label=""];
--     3 [label=""];
--     1 -> 2;
--     1 -> 3;
--     2 -> 3;
-- }
-- ...
unlabelGraph :: Ord v => Graph v -> Graph v
unlabelGraph  (Graph arcs labels styles)= (Graph arcs (mapWithKey f labels ) styles)
                                         where f  = adjust "" *the actual key*
我希望达到地图标签的每个值,并用“”替换它们。 如何在函数的where子句中指定需要映射的键? 这是编写此函数的最佳方法吗?

这应该可以:

import qualified Data.Map as M

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.mapWithKey f labels) styles)
   where f _key _value = ""
尽管如此,您实际上并不需要
f
中的键,因为它总是返回空字符串。因此,我们可以使用

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.map f labels) styles)
   where f _value = ""
甚至

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.map (const "") labels) styles)
unlabelGraph g = g{labelMap = M.map (const "") (labelMap g)}
甚至

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.map (const "") labels) styles)
unlabelGraph g = g{labelMap = M.map (const "") (labelMap g)}
这应该起作用:

import qualified Data.Map as M

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.mapWithKey f labels) styles)
   where f _key _value = ""
尽管如此,您实际上并不需要
f
中的键,因为它总是返回空字符串。因此,我们可以使用

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.map f labels) styles)
   where f _value = ""
甚至

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.map (const "") labels) styles)
unlabelGraph g = g{labelMap = M.map (const "") (labelMap g)}
甚至

unlabelGraph  (Graph arcs labels styles) =
   (Graph arcs (M.map (const "") labels) styles)
unlabelGraph g = g{labelMap = M.map (const "") (labelMap g)}

嘿,非常感谢您的回答,我尝试了以下代码
unlabelGraph::Ord v=>Graph v->Graph v unlabelGraph(Graph arcs labels style)=(Graph arcs(mapWithKey f labels)style),其中f key value=“sdhjfgkuewwhdoji”
我这样做是为了理解函数的功能,我不知道为什么它在值中不显示sdhjfgkuewhdoji​​我的标签映射(其为空)。你知道为什么吗?@VelyynS
map
/
可以用key
替换map中的现有值。如果从空贴图开始,结果仍然为空。也许你想要的是替换
arcs
map中的值,这样你就可以使用那些键而不是
labels
map中的键了?嘿,非常感谢你的回答,我尝试了以下代码
unlabelGraph::Ord v=>Graph v->Graph v unlabelGraph(Graph arcs labels styles)=(Graph arcs(mapWithKey f labels)样式)其中f key value=“sdhjfgkuewhdoji”
我这样做是为了理解函数的功能,我不知道为什么它在值中不显示sdhjfgkuewhdoji​​我的标签映射(其为空)。你知道为什么吗?@VelyynS
map
/
可以用key
替换map中的现有值。如果从空贴图开始,结果仍然为空。也许您需要替换
映射中的值,以便使用这些键而不是
标签
映射中的键?