简体   繁体   English

无法将预期类型`Bool'与实际类型`Card - > Bool'匹配

[英]Couldn't match expected type `Bool' with actual type `Card -> Bool'

There is my code: 有我的代码:

data Suit = Spade | Heart | Diamond | Club deriving (Eq, Show)
data CVal = Ace | King | Queen | Jack | Ten | Nine | Eight | Seven | Six | Five | Four | Three | Two deriving Show
data Card = Card Suit CVal deriving Show

sameSuit :: Card -> Card -> Bool
sameSuit (Card Spade _) (Card Spade _)     = True
sameSuit (Card Heart _) (Card Heart _)     = True
sameSuit (Card Diamond _) (Card Diamond _) = True
sameSuit (Card Club _) (Card Club _)       = True
sameSuit (Card x _) (Card y _)             = False

getNumber :: Card -> Int
getNumber (Card _ Two)   = 2
getNumber (Card _ Three) = 3
getNumber (Card _ Four)  = 4
getNumber (Card _ Five)  = 5
getNumber (Card _ Six)   = 6
getNumber (Card _ Seven) = 7
getNumber (Card _ Eight) = 8
getNumber (Card _ Nine)  = 9
getNumber (Card _ Ten)   = 10
getNumber (Card _ Jack)  = 11
getNumber (Card _ Queen) = 12
getNumber (Card _ King)  = 13
getNumber (Card _ Ace)   = 14

beats :: Card -> Card -> Bool
beats x y = if sameSuit (x y) && getNumber(x) > getNumber(y) then True else False

Error message: 错误信息:

Couldn't match expected type `Bool' with actual type `Card -> Bool'
In the return type of a call of `sameSuit'
In the first argument of `(&&)', namely `sameSuit (x y)'
In the expression: sameSuit (x y) && getNumber (x) > getNumber (y)

I don't understand why i can't call function "sameSuit" in finction "beats". 我不明白为什么我不能在“节拍”中调用功能“sameSuit”。 If I call it from prelude, like prelude > sameSuit (Card Club 10) (Card Club Ace) it returns right value and function type is Bool, not "Card -> Bool". 如果我从前奏中调用它,比如前奏曲> sameSuit(Card Club 10)(Card Club Ace),它返回正确的值和功能类型是Bool,而不是“Card - > Bool”。 What i do wrong? 我做错了什么? Can someone explain it to me? 有人可以向我解释一下吗?

In beats you appear to be trying to call several functions as function(args) , using C-like syntax. beats您似乎试图将多个函数称为function(args) ,使用类似C的语法。 Haskell does not use this syntax; Haskell不使用这种语法; function application is written by simple token adjacency, with parentheses only needed for nested expressions. 函数应用程序由简单的标记邻接编写,只有嵌套表达式才需要括号。

getNumber(x) is harmless, but pointless. getNumber(x)是无害的,但没有意义。 It is parsed as the application of a function getNumber to a parenthesised expression (x) , which is of course equivalent to just getNumber x , so it does what you want. 它被解析为函数getNumber到括号表达式(x) ,这当然等于getNumber x ,所以它可以做你想要的。

sameSuit (xy) is parsed as the application of a function sameSuit to a single argument, the parenthesised expression (xy) . sameSuit (xy)被解析为函数sameSuit应用于单个参数,括号表达式(xy) The sub-expression is in turn the application of a function x to y , which makes no sense in this context as x is a Card , not a function. 子表达式又是函数xy的应用,在这种情况下没有意义,因为xCard ,而不是函数。 You need to supply two arguments for sameSuit , as in sameSuit xy . 你需要为sameSuit提供两个参数,就像在sameSuit xy

Since sameSuit is of type Card -> Card -> Bool , sameSuit supplied with only a single argument is of type Card -> Bool . 由于sameSuit属于Card -> Card -> BoolsameSuit只提供一个参数的sameSuit属于Card -> Bool This is the error the compiler is reporting to you; 这是编译器向您报告的错误; you obviously can't && a function and a Bool . 你显然不能&&一个功能和Bool

If the compiler checked things in a different order it would also tell you that xy is not of type Card , and that x is not a function. 如果编译器以不同的顺序检查事物,它也会告诉你xy不是Card类型,而x不是函数。

A corrected implementation of beats is: 纠正的beats实施是:

beats x y = (sameSuit x y) && (getNumber x > getNumber y)

So, you can call sameSuit in beats , but you have to use parens. 所以,你可以beats调用sameSuit ,但你必须使用parens。

Edit : Actually, you don't need the parens. 编辑 :实际上,你不需要parens。 In your code you were calling sameSuit (xy) , but you have to call it without parens: sameSuit xy 在您的代码中,您调用的是sameSuit (xy) ,但是您必须在没有parens的情况下调用它: sameSuit xy

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

相关问题 无法将预期类型`Bool'与实际类型`IO Bool'匹配 - Couldn't match expected type `Bool' with actual type `IO Bool' 无法将预期类型“Bool”与实际类型“Char -> Bool”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘Char -> Bool´ 无法将预期类型“Bool”与实际类型“a -> Bool”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘a -> Bool’ 无法将预期类型'Integer - > t'与实际类型'Bool'匹配 - Couldn't match expected type ‘Integer -> t’ with actual type ‘Bool’ 无法将预期类型“Bool”与实际类型“[[Integer]] 匹配 - Couldn't match expected type `Bool' with actual type `[[Integer]] 无法将预期类型“布尔”与实际类型“ Int”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘Int’ haskell 无法将预期类型与实际类型“Bool”匹配 - haskell couldn't match expected type with actual type 'Bool' 无法将预期的类型“布尔”与实际类型“(a,a)”匹配 - Couldn't match expected type ‘Bool’ with actual type ‘(a, a)’ 无法将预期类型“[Integer]”与实际类型“Bool”匹配 - Couldn't match expected type `[Integer]' with actual type `Bool' 无法将预期的类型'(Char,Bool)-> Bool'与实际类型'Char'相匹配 - Couldn't match expected type ‘(Char, Bool) -> Bool’ with actual type ‘Char’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM