YesNo a where"代码行异常-6ren"> YesNo a where"代码行异常-这很好用: class YesNo a where yesNo :: a-> Bool instance YesNo Bool where yesNo True = True -6ren">
gpt4 book ai didi

haskell - "instance (Num a) => YesNo a where"代码行异常

转载 作者:行者123 更新时间:2023-12-02 06:57:52 25 4
gpt4 key购买 nike

这很好用:

class YesNo a where
yesNo :: a-> Bool

instance YesNo Bool where
yesNo True = True
yesNo _ = False

instance YesNo [a] where
yesNo [] = False
yesNo _ = True

instance YesNo (Maybe a) where
yesNo Nothing = False
yesNo _ = True

但是我收到代码错误:

instance (Num a) => YesNo a where -- error is here
yesNo 0 = False
yesNo _ = True

异常信息:

ghci> :l src
[1 of 1] Compiling Main ( src.hs, interpreted )

src.hs:16:21:
Illegal instance declaration for `YesNo a'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for `YesNo a'
Failed, modules loaded: none.
ghci>

我做错了什么?

最佳答案

看来您正在尝试使作为 Num 实例的每个类型自动成为 YesNo 的实例。

很遗憾,您不能那样做。

您只能为特定类型 声明一个实例。因此,您可以为 IntDouble 声明一个实例,但不能为“每个 Num”声明一个实例。

关于haskell - "instance (Num a) => YesNo a where"代码行异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27566373/

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