Alex
Alex

Reputation: 667

How to check if a list of booleans contains a value?

How to check if a list of Booleans contains True?

eg

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

Upvotes: 3

Views: 13543

Answers (5)

Roopesh Shenoy
Roopesh Shenoy

Reputation: 3447

You can always check for existence of standard functions in Hoogle.

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

Gives you several functions, out of which or is the one for this requirement.

EDIT:

or is a function. Its signature, or :: [Bool] -> Bool means it takes in a list of Bool and returns a Bool.

so, just doing

myList = [True, False, False]

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

might be how you will use this in your code (replace ..something.. and ..something else.. with actual expressions).

Upvotes: 7

Ingo
Ingo

Reputation: 36339

There are several funny ways to do that:

or
foldl (||) False
any id
not . all not
...

Upvotes: 1

Frerich Raabe
Frerich Raabe

Reputation: 94529

The generic way to check if a list contains some value is to use elem as in

Prelude> True `elem` [True, False]
True
Prelude> True `elem` [False, False]
False
Prelude> True `elem` [False, False, True]
True

Upvotes: 1

wit
wit

Reputation: 1622

Try to use Hoogle or Hayoo as search engines. And surf Platform libs for answer.

Reply is in the Prelude:

or :: [Bool] -> Bool

Upvotes: 0

bheklilr
bheklilr

Reputation: 54078

You're looking for the or function:

> ghci

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

There's also the and function which returns True if all the elements of the list are True.

Upvotes: 7

Related Questions