更新时间:2021-11-30 07:20:25
我相信您想检查是否同时存在(<5)
和(>3)
的任何元素.
I believe you'd like to check whether there are any elements that are both (<5)
and (>3)
.
您可以这样操作:
any (\x -> x > 3 && x < 5) [1..5]
您的另一个可以通过
any (\x -> x < 2 || x > 4) [1..5]
但是定义&&
和||
以便在函数上工作可能会更有趣:
But maybe it would be more fun to define &&
and ||
to work on functions:
infixr 3 &&&
infixr 3 |||
(&&&) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
(f &&& g) x = f x && g x
(|||) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
(f ||| g) x = f x || g x
所以现在我们可以将您的示例重写为:
so now we can rewrite your examples as:
any ((>3) &&& (<5)) [1..5]
any ((<2) ||| (>4)) [1..5]