gpt4 book ai didi

haskell - Haskell 中如何定义 (==) ?

转载 作者:行者123 更新时间:2023-12-04 13:02:09 25 4
gpt4 key购买 nike

我正在用 Haskell 编写一种小型函数式编程语言,但我找不到 (==) 如何实现的定义,因为这似乎很棘手?

最佳答案

Haskell 使用“类型类”的概念。实际的定义是这样的:

class Eq a where
(==) :: a -> a -> Bool
-- More functions follow, for more complex concepts of equality (eg NaN)

然后你可以为你自己的类型定义它。例如:
-- Eq can't be automatically derived, because of the function
data Foo = Foo Int (Char -> Bool)

-- So define it here
instance Eq Foo where
(Foo x _) == (Foo y _) = x == y

关于haskell - Haskell 中如何定义 (==) ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1546031/

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