简体   繁体   English

无法将预期类型与实际类型匹配

[英]Couldn't match expected type with actual type

I've recently started learning Haskell and is having trouble figuring out what is wrong with my code but ended up in failure. 我最近开始学习Haskell,在弄清楚我的代码有什么问题时遇到了麻烦,但最终失败了。

This is part of my code showing that I've declared data Constructor for Graph type, which is a list of tuple of a value and a list of that value. 这是我的代码的一部分,显示我已声明Graph类型的数据构造函数,该数据构造函数是一个值的元组列表和该值的列表。

data Graph a = Graph [(a,[a])] deriving (Ord, Eq, Show)

This is type synonym for tuple of two ints, 这是两个整数的元组的类型同义词,

type Point = (Int, Int)

And finally this code is to find second argument of a graph using first argument(ie searching / hashing value) 最后,此代码是使用第一个参数(即搜索/哈希值)查找图的第二个参数

pointNeighbor :: Graph Point -> Point -> [Point]
pointNeighbor (x:xs) point = if fst x == point then snd x else pointNeighbor (xs) point

This is the error message I get when I try to load module 这是我尝试加载模块时收到的错误消息

hw1.hs:37:16: Couldn't match expected type 'Graph Point' with actual type '[(Point, [Point])]' In the pattern: x : xs In an equation for 'pointNeighbor': pointNeighbor (x : xs) point = if fst x == point then snd x else pointNeighbor (xs) point hw1.hs:37:16:无法将预期的类型'Graph Point'与实际类型'[[(Point,[Point])]''相匹配在模式中:x:xs在'pointNeighbor'的方程式中:pointNeighbor(x :xs)point =如果fst x == point然后snd x else pointNeighbor(xs)point

hw1.hs:37:79: Couldn't match expected type 'Graph Point' with actual type '[(Point, [Point])]' In the first argument of 'pointNeighbor', namely '(xs)' In the expression: pointNeighbor (xs) point hw1.hs:37:79:无法将预期类型'Graph Point'与实际类型'[[(Point,[Point])]''相匹配'pointNeighbor'的第一个参数,即'(xs)' :pointNeighbor(xs)点

It seems like Graph Point should be recognized as [(Point,[Point])] but apparently it is giving me this error and I can't find any solution on web. 似乎Graph Point应该被识别为[[Point,[Point])],但显然这给了我这个错误,我在网络上找不到任何解决方案。

Thanks in advance :) 提前致谢 :)

Your function expects the first argument to be of type Graph . 您的函数期望第一个参数的类型为Graph As Graph is defined, there is only one way to do this: with the Graph value constructor (the ocurrence of the word 'Graph' on the right hand side of the = in the data definition). 定义Graph ,只有一种方法可以实现:使用Graph值构造函数( data定义中=右侧出现 'Graph'一词)。

However, the pattern match you are trying to perform pretends like the first argument is plain List , instead of a List that has been made part of the Graph value constructor. 但是,您要假装执行的模式匹配像第一个参数一样是Plain List ,而不是已经成为Graph值构造函数一部分的List

The pattern match should be for (Graph (x:xs)) and also the recursive call in the else clause should use (Graph xs) like this: 模式匹配应针对(Graph (x:xs))并且else子句中的递归调用也应使用(Graph xs)如下所示:

pointNeighbor :: Graph Point -> Point -> [Point]
pointNeighbor (Graph (x:xs)) point = 
    if fst x == point 
        then snd x 
        else pointNeighbor (Graph xs) point

Note also that you don't define a base case when the list is empty. 还请注意,列表为空时,您无需定义基本情况。

Because Graph is a data constructor, you have to pattern match against it: 由于Graph是数据构造函数,因此必须对其进行模式匹配:

pointNeighbor :: Graph Point -> Point -> [Point]
pointNeighbor (Graph (x:xs)) point = if fst x == point then snd x else pointNeighbor (Graph xs) point
               ^^^^^                                                                  ^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM