Haskell 元组大小限制 [英] Haskell Tuple Size Limit

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

问题描述

为什么我不能在 Haskell 中构造大元组?为什么有元组大小限制?

Why I can't construct large tuples in Haskell? Why there's a tuple size limit?

Prelude> (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

<interactive>:1:0:
    No instance for (Show
                       (t,
                        t1,
                        t2,
                        ...
                        t23))
      arising from a use of `print' at <interactive>:1:0-48
    Possible fix:
      add an instance declaration for
      (Show
         (t,
          t1,
          t2,
          ...
          t23))
    In a stmt of a 'do' expression: print it

推荐答案

元组可以是任意长度*,但 Show 以及 Eq、Ord、Read、Bounded 等只能实例化最多 15 个元组.来自 Haskell 98 报告 §6.1.4:

Tuples can be of arbitrary length*, but Show, as well as Eq, Ord, Read, Bounded, etc are only instantiated up to 15-tuple. From the Haskell 98 report §6.1.4:

元组的大小没有上限,但是一些 Haskell 实现可能会限制元组的大小,并限制与较大元组关联的实例.但是,每个 Haskell 实现都必须支持最大 15 的元组,以及 Eq、Ord、Bounded、Read 和 Show 的实例. Prelude 和库定义了元组函数,例如 zip 的元组最大为大小为 7.

There is no upper bound on the size of a tuple, but some Haskell implementations may restrict the size of tuples, and limit the instances associated with larger tuples. However, every Haskell implementation must support tuples up to size 15, together with the instances for Eq, Ord, Bounded, Read, and Show. The Prelude and libraries define tuple functions such as zip for tuples up to a size of 7.

正如其他人所说,如果你需要一个 24 元组,你应该使用更好的数据结构.

As others have said, if you need a 24-tuple, you should use a better data structure.

* 自 GHC 6.12.2 起,元组的最大大小为 62:

* as of GHC 6.12.2, the maximum size of a tuple is 62:

Prelude> :t (1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8)

<interactive>:1:0:
    A 64-tuple is too large for GHC
      (max size is 62)
      Workaround: use nested tuples or define a data type

这篇关于Haskell 元组大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆