gpt4 book ai didi

haskell - Bool 类型的排序(即 True > False) - 为什么?

转载 作者:行者123 更新时间:2023-12-03 14:55:35 29 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Understanding Haskell's Bool Deriving an Ord

(3 个回答)


6年前关闭。




有人可以解释以下输出吗?

Prelude> compare True False
GT
it :: Ordering
Prelude> compare False True
LT
it :: Ordering

为什么是 Bool在 Haskell 中排序的类型值 - 特别是因为我们可以证明 True 和 False 的值不完全是 1 和 0(与许多其他语言不同)?

最佳答案

这就是 Ord 的派生实例的方式作品:

data D = A | B | C deriving Ord

给定该数据类型,我们得到 C > B > A . bool 定义为 False | True ,当您查看其他示例时,它是有道理的,例如:
  • Maybe a = Nothing | Just a
  • Either a b = Left a | Right b

  • 在每种情况下,具有“某些”(“真实”)值的值都大于根本没有值(或具有“左”或“坏”或“假”值)。

    关于haskell - Bool 类型的排序(即 True > False) - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692693/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com