此函数将返回一个Either
类型。String 表示错误值,Right/Bool 表示用户关心的普通布尔类型
testPre :: Rational -> Rational -> Either String Bool
testPre a b
| b == 0 = Left "Shouldn't be 0"
| a > b = Right True
| a <= b = Right False
any
但是,如果我想测试带有特殊或all
可与 String Bool 一起使用的数字列表,该怎么办? 使any
或all
在 的上下文中工作Either
?
all (testPre 3) [1,2] => should be `Right True`
all (testPre 3) [1,2,3] => should be `Right False`
any (testPre 3) [1,5] => should be `Right True`
any (testPre 3) [5,6] => should be `Right False`
any (testPre 3) [1,0,3] => should be "Shouldn't be 0"
谢谢