【发布时间】:2018-12-22 21:59:05
【问题描述】:
是否有理由解释这样的结果是否符合预期?它们比未定义更好吗?
>>> 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
【问题讨论】:
-
好吧
and基本上是为所有元素制作fold和&&,但最初需要True(因为否则结果总是False,无论值如何) . -
这是跨语言的标准处理。例如,here's the same question in Java。这个概念被称为“空洞的真相”。
-
请注意,出于同样的原因,
sum [] == 0和product [] == 1。
标签: haskell