-1

我在 Haskell 中有一个 10 元组的列表,我想从该元组列表中获取第 n 个元组。但正如我所见,只有长度函数适用于该列表。功能不起作用。你能告诉我我该怎么做吗?元组由整数和字符串组成。例如,当我尝试这个时:

tail [(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8")]

我从拥抱中收到此错误消息:

ERROR - Cannot find "show" function for:
*** Expression : tail [(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8"),(3,5,"String1","String2","String3","String4","String5","String6","String7","String8")]
*** Of type    : [(Integer,Integer,[Char],[Char],[Char],[Char],[Char],[Char],[Char],[Char])]
4

1 回答 1

5

以下是如何Show为 3 元组声明实例。希望这说明了这个想法,您可以将其扩展到更多元素:

导入 Data.List(插入)

实例 (显示 a, 显示 b, 显示 c) => 显示 (a, b, c) 其中
  show (a, b, c) = "(" ++ (intercalate "," ([show a, show b, show c])) ++ ")"

您可以像逻辑含义一样阅读实例声明:如果我可以显示类型 a、b 和 c 的值,那么我可以显示类型为 (a、b、c) 的元组,这就是方法。

GHC 为最多 15 个元组的所有内容定义了一个 Show 实例,因此您可能不需要在您的情况下定义它。

于 2013-03-21T23:03:50.647 回答