问题描述
我想定义一个元组 (x, y)
作为 Enum
类的实例,知道 x
和 y
是 Enum
的实例.以下尝试:
I'd like to define a tuple (x, y)
as an instance of Enum
class, knowing that both x
and y
are instances of Enum
. A following try:
instance (Enum x, Enum y) => Enum (x, y) where
toEnum = y
enumFrom x = (x, x)
只会导致错误(y
不在范围内).我是 Haskell 的新手,有人能解释一下如何声明这样的实例吗?
only results in error (y
not in scope). I'm new to Haskell, could somebody explain how to declare such an instance?
推荐答案
instance (Enum x, Enum y) => Enum (x, y) where
在上一行中,x
和 y
都是类型(类型变量).
In the above line, x
and y
are both types (type variables).
toEnum = y
enumFrom x = (x, x)
在上面两行中,x
和 y
都是值((value) 变量).y
-as-a-value 没有在任何地方定义,这就是它不在范围内的意思.
In the above two lines, x
and y
are both values ((value) variables). y
-as-a-value has not been defined anywhere, that's what it not being in scope means.
至于如何声明这样的实例,我不确定您希望 fromEnum
和 toEnum
的行为方式,例如.
As to how to declare such an instance, I'm not sure how you'd want fromEnum
and toEnum
to behave, for example.
这篇关于Haskell 中元组的枚举实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!