【发布时间】:2018-03-19 15:47:07
【问题描述】:
我正在尝试验证来自字符串的整数输入,如果输入字符串正确地将类型更改为整数,我只需要一个布尔结果。我从另一个问题尝试了这种方法:
https://stackoverflow.com/a/30030649/3339668
这是相关代码以及我的导入:
import Data.Char
import Data.List
import Text.Read
checkValidInt :: String -> Bool
checkValidInt strInt
| readMaybe strInt :: Maybe Int == Nothing = False
| readMaybe strInt :: Maybe Int /= Nothing = True
但是,我在加载脚本时收到以下错误:
Illegal operator ‘==’ in type ‘Maybe Int == Nothing’
Use TypeOperators to allow operators in types
main.hs:350:38:
Not in scope: type constructor or class ‘Nothing’
A data constructor of that name is in scope; did you mean DataKinds?
main.hs:351:35:
Illegal operator ‘/=’ in type ‘Maybe Int /= Nothing’
Use TypeOperators to allow operators in types
main.hs:351:38:
Not in scope: type constructor or class ‘Nothing’
A data constructor of that name is in scope; did you mean DataKinds?
那么什么数据类型是Nothing呢?如何检查 Nothing 是否是 readMaybe 的正确结果?
谢谢。
【问题讨论】:
-
你根本不应该在这里使用
==;虽然Maybe a有一个Eq实例,但您不需要 在这里使用它。模式匹配就足够了。
标签: validation haskell