0

假设我有以下列表

[((Int, Int), [[Char]], [[Char]], Bool, Bool, Bool)]
[(Int, Int, [Char], [Char], [Char], [Char], [Char], [Char], [Char], [Char])]

我知道为了能够打印它们,我需要将它们定义为新的数据类型,然后创建一个 Show 实例,但有什么办法可以避免它?

提前致谢。

4

1 回答 1

3

让我们替换所有的[Char]byString以摆脱一些括号重载:

[((Int, Int), [String], [String], Bool, Bool, Bool)]

[(Int, Int, String, String, String, String, String, String, String, String)]

这些已经可以打印:

Prelude> let x = replicate 2 ((1,2), ["a", "b"], ["c", "d"], True, False, True) :: [((Int,Int), [String], [String], Bool, Bool, Bool)]

Prelude> :type x   -- displays the type of x
x :: [((Int, Int), [String], [String], Bool, Bool, Bool)]

Prelude> print x
[((1,2),["a","b"],["c","d"],True,False,True),((1,2),["a","b"],["c","d"],True,False,True)]

Prelude> let x = replicate 2 (1,2,"a","b","c","d","e","f","g","h") :: [(Int,Int,String,String,String,String,String,String,String,String)]

Prelude> :type x
x :: [(Int,
       Int,
       String,
       String,
       String,
       String,
       String,
       String,
       String,
       String)]

Prelude> print x
[(1,2,"a","b","c","d","e","f","g","h"),(1,2,"a","b","c","d","e","f","g","h")]
于 2013-04-02T08:00:18.693 回答