2

如何检查布尔值列表是否包含 True?

例如

[True,False] = True
[False,False] = False
[False,False,True] = True
4

5 回答 5

6

您正在寻找以下or功能:

> ghci

Prelude> or [True, False]
True
Prelude> or [False, False]
False

如果列表的所有and元素True都是True.

于 2013-10-07T14:19:10.883 回答
6

您可以随时检查 Hoogle 中是否存在标准函数。

例如:http ://www.haskell.org/hoogle/?hoogle=%5BBool%5D+-%3E+Bool

为您提供了几个功能,其中or一个是满足此要求的功能。

编辑:

or是一个函数。它的签名or :: [Bool] -> Bool意味着它接受Bool并返回一个Bool.

所以,只是在做

myList = [True, False, False]

if (or myList) then ..something.. else ..something else.. 

可能是您在代码中使用它的方式(将 ..something.. 和 ..something else.. 替换为实际表达式)。

于 2013-10-07T14:21:45.950 回答
1

检查列表是否包含某些值的通用方法是elem使用

Prelude> True `elem` [True, False]
True
Prelude> True `elem` [False, False]
False
Prelude> True `elem` [False, False, True]
True
于 2013-10-07T14:24:08.913 回答
1

有几种有趣的方法可以做到这一点:

or
foldl (||) False
any id
not . all not
...
于 2013-10-07T15:07:42.197 回答
0

尝试使用HoogleHayoo作为搜索引擎。并浏览平台库以获得答案。

回复在前奏中:

or :: [Bool] -> Bool
于 2013-10-07T14:20:57.237 回答