我尝试打印以下元组
(1,2,3,4,5,6)
给了我以下错误
ERROR - Cannot find "show" function for:
*** Expression : (1,2,3,4,5,6)
*** Of type : (Integer,Integer,Integer,Integer,Integer,Integer)
但是,如果我尝试使用以下元组
(1,2,3,4,5)
一切都像魅力一样。
我的猜测是Haskell只为最多五个元素的元组定义Show
(因为具有5个元素的元组显然是另一个类型而不是具有6个元素的元组)。但不知何故,我很愚蠢地找到证据。
所以我的问题是:
为什么会如此,如何打印带有6个元素的元组(因为我似乎无法将元组放在instance
定义的头部)?
编辑:我正在使用Hugs
答案 0 :(得分:7)
不要使用Hugs。
它已经多年没有维护,并且不支持现代Haskell实现应该支持的一半的东西。 (Hugs支持“大部分”Haskell '98,这是语言规范的先前版本。)
您可能会在互联网上找到与GHC完美配合的示例代码,但与Hugs一起失败。你会花费数年时间来弄清楚为什么你的代码不起作用,只是为了最终意识到“哦,它 工作 - 只是没有Hugs”。
答案 1 :(得分:1)
以下是我对ghci
Prelude> (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,5 1,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100)
<interactive>:445:1:
A 100-tuple is too large for GHC
(max size is 62)
Workaround: use nested tuples or define a data type
然而,对于打印15是限制
Prelude> (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
<interactive>:450:1:
No instance for (Show
(t0,
t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15))
arising from a use of `print'
Possible fix:
add an instance declaration for
(Show
(t0,
t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15))
In a stmt of an interactive GHCi command: print it
答案 2 :(得分:1)
你走了。包括这个显示为6元组的实例
-- Tuples -- for ref, def'n from the Prelude
{-
instance (Show a, Show b) => Show (a,b) where
showsPrec p (x,y) = showChar '(' . shows x . showChar ',' .
shows y . showChar ')'
instance (Read a, Read b) => Read (a,b) where
readsPrec p = readParen False
(\r -> [((x,y), w) | ("(",s) <- lex r,
(x,t) <- reads s,
(",",u) <- lex t,
(y,v) <- reads u,
(")",w) <- lex v ] )
-}
-- Other tuples have similar Read and Show instances
instance (Show a, Show b, Show c, Show d, Show e, Show f)
=> Show (a,b,c,d,e,f) where
showsPrec p (x1,x2,x3,x4,x5,x6) = showChar '(' . shows x1 . showChar ',' .
shows x2 . showChar ',' .
shows x3 . showChar ',' .
shows x4 . showChar ',' .
shows x5 . showChar ',' .
shows x6 . showChar ')'