我在做haskell中的map reduce,并且给出了一些代码来开始,但我得到一些编译器错误,不理解的形式为:无法在地图缩减过程中从上下文(Ord k2)中推导出(k2〜k4)
无法推断(K2〜K4)从上下文 (ORD K2)
下面的代码:
import Data.Map (Map,empty,insertWith,mapWithKey,filterWithKey,toList)
mapReduce :: forall k1 k2 v1 v2 v3. Ord k2
=> (k1 -> v1 -> [(k2,v2)])
-> (k2 -> [v2] -> Maybe v3)
-> Map k1 v1
-> Map k2 v3
mapReduce mAP rEDUCE = reducePerKey . groupByKey . mapPerKey
where
mapPerKey :: Map k1 v1 -> [(k2,v2)]
mapPerKey =
concat
. map (uncurry mAP)
. toList
groupByKey :: [(k2,v2)] -> Map k2 [v2]
groupByKey = foldl insert empty
where
insert dict (k2,v2) = insertWith (++) k2 [v2] dict
reducePerKey :: Map k2 [v2] -> Map k2 v3
reducePerKey =
mapWithKey unJust
. filterWithKey isJust
. mapWithKey rEDUCE
where
isJust k (Just v) = True
isJust k Nothing = False
unJust k (Just v) = v
感谢您的帮助!
也许向我们展示完整的错误?也许还有完整的代码,因为你展示的内容中没有一个'k4'? – alternative 2014-11-01 22:51:26
还有在命令行中指定的任何扩展......特别是启用了“ScopedTypeVariables”? – 2014-11-01 23:00:52
@alternative'k4'只是GHC通过在其中添加/增加数字来区分不同版本的同名命名类型变量的习惯的结果。我不明白在代码中使用编号类型的变量。 (如果启用'RankNTypes',但* * * * ScopedTypeVariables',则可以看到错误。) – 2014-11-02 01:05:05