Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我可以在Haskell中打印包含5个元素但不包含6个元素的元组?_Haskell_Hugs - Fatal编程技术网

为什么我可以在Haskell中打印包含5个元素但不包含6个元素的元组?

为什么我可以在Haskell中打印包含5个元素但不包含6个元素的元组?,haskell,hugs,Haskell,Hugs,我试着打印下面的元组 (1,2,3,4,5,6) (1,2,3,4,5) 这给了我以下的错误 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,6) (1,2,3,4,5) 每件事都很有魅力 我的猜测是Hask

我试着打印下面的元组

(1,2,3,4,5,6)
(1,2,3,4,5)
这给了我以下的错误

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,6)
(1,2,3,4,5)
每件事都很有魅力

我的猜测是Haskell只为最多五个元素的元组定义了
Show
(因为有5个元素的元组显然是有6个元素的元组的另一种类型)。但不知何故,我太愚蠢了,无法找到证据证明这一点

所以我的问题是:

为什么会这样?我如何打印一个包含6个元素的元组(因为我似乎无法将元组放在
实例
定义的头部)


编辑:我正在使用拥抱

这是我对
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
[代码>序序幕<<<<<>序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序序(1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3(92,93,94,95,96,97,98,99100) :445:1: 100元组对于GHC来说太大了 (最大尺寸为62) 解决方法:使用嵌套元组或定义数据类型
但是,对于打印,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
序曲>(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
:450:1:
没有(表演)的例子
(t0,
t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15)
因使用“print”而产生的
可能的解决方案:
为添加实例声明
(表演
(t0,
t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15)
在交互式GHCi命令的stmt中:打印它
不要拥抱

它已经好几年没有被维护了,并且没有支持现代Haskell实现应该支持的一半内容。(Hugs支持Haskell'98的“most of”,这是语言规范的早期版本。)


您可能会在互联网上找到与GHC完美配合的示例代码,但在拥抱时却失败得很惨。你会花很多时间试图找出你的代码为什么不起作用,但最终会意识到“哦,它确实起作用了——只是没有拥抱而已。”

给你。为6元组包含此Show实例

-- 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 ')'
--元组--用于前奏曲中的ref和def'n
{-
实例(Show a,Show b)=>Show(a,b)其中
showsPrec p(x,y)=showChar'('.shows x.showChar','。
显示y.showChar')'
实例(读a,读b)=>读(a,b),其中
readsPrec p=readParen False

(\r->[(x,y,w)|(“,s”)无法复制。您使用的是哪一版本的GHC?对我来说也是一样。好吧,Hugs没有维护。切换到GHCi是个好主意。也就是说:如果您的元组大小大于3,那么您可能做错了什么……我担心Hugs在这方面受到限制。对于一个今天已经过时的软件来说,这并不奇怪--它已经有很多年没有被维护了。根据,
Show
应该为最多有15个元素的元组实现。Hugs可能使用它自己版本的标准前奏。是的,GHCi有一个限制。Hugs也有一个限制,它只是更低。(我同意O.P.5个元素可以,6个不可以。)在Hugs中很容易找到在Prelude Show中定义实例的位置;复制代码;添加自己的实例。在GHC中没有这么多。Hugs对于Haskell的无痛起步非常有用。我仍然偶尔使用它(在iPad上),尽管使用GHC已有15年之久。GHC现在充斥着更高级的打字/打字,因此理解它的错误信息需要一个深奥数学博士学位。这对鼓励人们学习函数式编程不太好。我认为有一些介绍性的CompSci课程建议大家多学拥抱GHC@AntC: “提示:映射
编译器::ProgrammingLanguage->Program
可能不是内射的…”-是的,GHC的错误消息从来都不是对初学者最友好的,尤其是错误消息让情况变得更糟。但是,它们的时代正在发生变化——Haskell的许多卖点是,现在它实际上是一个竞争激烈、功能齐全的生态系统,由于堆栈,您可以在大多数平台上快速运行。与此相反学术的、漂亮的、简单的但不适合生产的解释程序,许多Lisp一直坚持使用。@leftaroundabout:(StackOverflow不是讨论这个问题的地方,但是…)是的,GHC在获得FP认可方面正处于领先地位,因为它具有强大的行业实力。我担心我们如何帮助新手体验Haskell编程的优雅(尤其是来自OO范式)。在Hugs中有效的东西在GHC中也会起作用,所以它是一个很好的学习平台。IMO。