Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `and` return True for empty foldable but `or` return False in Haskell? [duplicate]

Tags:

haskell

Is there a reason that would explain whether such results are to be expected? Are they better than being undefined?

>>> any (const True) []
False

>>> any (const False) []
False

>>> or []
False

>>> and []
True

I don't really understand what the Report is trying to say:

-- and returns the conjunction of a Boolean list. For the result to be
-- True, the list must be finite; False, however, results from a False
-- value at a finite index of a finite or infinite list. or is the
-- disjunctive dual of and. 
and, or     :: [Bool]    -> Bool
and         =  foldr    (&&)    True
or          =  foldr    (||)    False
like image 583
sevo Avatar asked Dec 05 '22 10:12

sevo


1 Answers

To expand on the answer by Dan, the conjunction of an empty list of truth values being true True allows you to extend the expected properties of conjunction to that case.

For instance, we would expect that,

and (xs ++ ys) = (and xs) && (and ys) 

and for all xs we have, xs = xs ++ [] so,

  and xs 
= and (xs ++ [])
= (and xs) && (and []) 

considering that and xs might be True or False it follows that,

True  = True  && (and [])
False = False && (and [])

Therefore we must have that and [] is True.

like image 151
Jorge Adriano Avatar answered Apr 29 '23 05:04

Jorge Adriano