[英]Haskell Couldn't match expected type involving maybe
I am currently learning Haskell and I have run into a problem but I don't really understand what is the problem. 我目前正在学习Haskell,遇到了问题,但是我不太了解问题所在。 When I compile the code below, it tells me "Couldnt match expected type 'integer' with actual type 'Maybe Integer'" for line 17:19 and again the same error for 17:27. 当我编译下面的代码时,它告诉我第17:19行“无法将期望的类型'integer'与实际类型'Maybe Integer'匹配”,而对于17:27则再次出现相同的错误。
maybe_divide :: Maybe Integer -> Maybe Integer -> Maybe Integer
maybe_divide a b = case valid_div a b of
True -> Just (a `div` b)
False -> Nothing
valid_div :: Maybe Integer -> Maybe Integer -> Bool
valid_div a b
| a == Nothing = False
| b == Nothing = False
| b == Just 0 = False
| otherwise = True
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢
In maybe_divide
, the types of a
and b
are Maybe Integer
.... You can not divide Maybe Integer
s 在maybe_divide
, a
和b
的类型为Maybe Integer
...。您不能将Maybe Integer
s maybe_divide
a `div` b --won't work
You will have to "unwrap" the Maybe
values, extract the Integer
s before you use them in div
. 您将需要“解开” Maybe
值,提取Integer
然后在div
使用它们。 (There are a few ways to do this, including pattern matching). (有几种方法可以做到这一点,包括模式匹配)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.