问题描述
为什么我不能在 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.
正如其他人所说,如果你需要一个 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 元组大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!