【发布时间】:2015-04-15 17:34:24
【问题描述】:
我最近开始学习 Haskell,无法弄清楚我的代码有什么问题,但最终失败了。
这是我的代码的一部分,显示我已经为 Graph 类型声明了数据构造函数,它是一个值的元组列表和该值的列表。
data Graph a = Graph [(a,[a])] deriving (Ord, Eq, Show)
这是两个整数元组的类型同义词,
type Point = (Int, Int)
最后这段代码是使用第一个参数(即搜索/散列值)找到图形的第二个参数
pointNeighbor :: Graph Point -> Point -> [Point]
pointNeighbor (x:xs) point = if fst x == point then snd x else pointNeighbor (xs) point
这是我尝试加载模块时收到的错误消息
hw1.hs:37:16: 无法匹配预期类型“图形点” 实际类型为‘[(Point, [Point])]’ 在模式中: x : xs 在“pointNeighbor”的方程中: pointNeighbor (x : xs) 点 = if fst x == point then snd x else pointNeighbor (xs) point
hw1.hs:37:79: 无法匹配预期类型“图形点” 实际类型为‘[(Point, [Point])]’ 在‘pointNeighbor’的第一个参数中,即‘(xs)’ 表达式中:pointNeighbor (xs) 点
似乎 Graph Point 应该被识别为 [(Point,[Point])] 但显然它给了我这个错误,我在网上找不到任何解决方案。
提前致谢:)
【问题讨论】:
标签: haskell