哈斯克尔元组大小限制 [英] Haskell 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:
没有显示
(t,
t1,
t2,
.. 。
t23))
在< interactive>使用'print'时产生:1:0-48
可能的修正:
为$ b $添加实例声明b(显示
(t,
t1,
t2,
...
t23))
在'do'表达式的结尾处:打印它
元组可以是任意长度*,但显示,以及Eq,Ord,Read,Bounded等只能实例化到15元组。从 Haskell 98 report§ 6.1.4 :
元组大小没有上限,但是一些Haskell实现可能会限制元组的大小,并限制与更大的元组关联的实例。然而,每个Haskell实现都必须支持最大大小为15的元组,以及Eq,Ord,Bounded,Read和Show的实例。 Prelude和库定义了元组函数,例如zip元组大小为7。
正如其他人所说的,如果你需要24元组,你应该使用更好的数据结构。 / p>
编辑:*从GHC 6.12.2开始,元组的最大大小为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:
64元组对于GHC
太大(最大值为62)
解决方法:使用嵌套元组或定义数据类型
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
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:
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.
As others have said, if you need a 24-tuple, you should use a better data structure.
Edit: * 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
这篇关于哈斯克尔元组大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!