中元组的枚举实例

中元组的枚举实例

本文介绍了Haskell 中元组的枚举实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个元组 (x, y) 作为 Enum 类的实例,知道 xyEnum 的实例.以下尝试:

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

在上一行中,xy 都是类型(类型变量).

In the above line, x and y are both types (type variables).

    toEnum = y
    enumFrom x = (x, x)

在上面两行中,xy 都是值((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.

至于如何声明这样的实例,我不确定您希望 fromEnumtoEnum 的行为方式,例如.

As to how to declare such an instance, I'm not sure how you'd want fromEnum and toEnum to behave, for example.

这篇关于Haskell 中元组的枚举实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:35