是否有原因可以解释这种结果是否可以预期?它们比未定义更好吗?
>>> any (const True) []
False
>>> any (const False) []
False
>>> or []
False
>>> and []
True
我不太了解报告要说的话:
-- 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
答案 0 :(得分:8)
要扩展Dan的答案,将真值为空的真值True
的空列表的连词允许您将连词的期望属性扩展到该情况。
例如,我们期望
and (xs ++ ys) = (and xs) && (and ys)
对于所有xs
,xs = xs ++ []
都是
and xs
= and (xs ++ [])
= (and xs) && (and [])
考虑到and xs
可能是True
或False
,
True = True && (and [])
False = False && (and [])
因此,我们必须确保and []
是True
。
答案 1 :(得分:7)
对于大多数具有类似功能的语言,这都是正确的。这是因为“ True”是“ &&”(即x && True == x)的标识。同样,“ False”是“ ||”的标识(含义x || False == x)。