[英]Couldn't match expected type ‘Bool’ with actual type ‘(a, a)’
I've got an issue with Haskell where it appears to be disliking the fact that I'm returning a Boolean statement from a helper function. 我在Haskell遇到了一个问题,它似乎不喜欢我从帮助程序函数返回布尔语句的事实。 I'm checking if the distance between two numbers in a list is very small (less than 0.01);
我正在检查列表中两个数字之间的距离是否很小(小于0.01); and in the case where it is true, I return this number.
如果是真的,我会传回这个号码。 In the case where it is false, I check the next two numbers in the list.
在错误的情况下,我检查列表中的下两个数字。
Code: 码:
positionChecker Pos ns = if compareDistance(ns !! Pos, ns !! (Pos+1))
then Pos
else positionChecker(Pos+1) ns
compareDistance n1 n2 = if abs(n1 - n2) < 0.01
then True
else False
(NB: I've removed the preceeding code that builds the list and calls positionChecker, initialising it with position 0, and a list of numbers) (注意:我删除了构建列表并调用positionChecker的先前代码,并使用位置0和数字列表对其进行了初始化)
The above is returning the following error; 以上返回以下错误;
pchk.hs:9:49:
Couldn't match expected type ‘Bool’ with actual type ‘(a, a)’
Relevant bindings include
ns :: [a] (bound at pchk.hs:9:21)
positionChecker' :: Int -> [a] -> a (bound at pchk.hs:9:1)
In the first argument of ‘compareDistance’, namely
‘(ns !! Pos, ns !! (Pos + 1))’
In the expression:
compareDistance (ns !! Pos, ns !! (Pos + 1))
In the expression:
if compareDistance (ns !! Pos, ns !! (Pos + 1)) then
ns !! (Pos + 1)
else
positionChecker' (Pos + 1) ns
Again, from what I can make it, haskell seems to be confused by the fact that compareDistance is returning a Bool type. 同样,根据我的能力,haskell似乎对compareDistance返回Bool类型这一事实感到困惑。
I understand that there are far more sensible approaches to this solution (inclusive of simple, single-function or single-line solutions); 我了解,对此解决方案有更明智的方法(包括简单的单功能或单行解决方案); but I'm just trying to make sense of the above error so that I can learn where I'm going wrong in this type of approach to a problem.
但是我只是想弄清楚上面的错误,以便我可以了解这种问题解决方法的错误之处。
Some comments on seeing your code: 关于查看代码的一些评论:
positionChecker Pos ns = ...
!!
!!
function, it's unsafe: λ> [0,1] !! 3 *** Exception: Prelude.(!!): index too large
Always write type signature for your functions. 始终为您的函数编写类型签名。 That will help you in debugging your problem more easily.
这将帮助您更轻松地调试问题。
Haskell has the concept currying. Haskell的概念容易变。 So you don't pass parameters like this:
因此,您不会传递这样的参数:
compareDistance(ns !! Pos, ns !! (Pos+1))
but like this: 但是像这样:
compareDistance (ns !! Pos) (ns !! (Pos+1))
You are calling "compareDistance" with a tuple as parameter instead of giving it two arguments. 您正在使用元组作为参数调用“ compareDistance”,而不是为其提供两个参数。 Remove the brackets and the coma.
卸下支架和昏迷。 It's Haskell, not C.
是Haskell,不是C。
This isn't how you call functions in Haskell. 这不是您在Haskell中调用函数的方式。 You don't use parentheses to call functions.
您不使用括号来调用函数。 What these parentheses actually do is turn the two values into a tuple, and then that tuple is passed as a single argument to the function, resulting in total chaos for the type checker.
这些括号的实际作用是将两个值变成一个元组,然后将该元组作为单个参数传递给函数,从而导致类型检查器完全混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.