我定义了以下类型:
data Taula = Taula String [[String]]
我创建了以下实例:
instance Show Taula
where
show Taula [( n , i , c )] = show Taula [( n , i , c )]++ "\n"
show ( l : ls ) = show l ++ "\n" ++ show ls
但它会产生以下错误:
Equations for `show' have different numbers of arguments
lastHaskell.hs:184:18-82
lastHaskell.hs:185:18-63
In the instance declaration for `Show Taula'
然后尝试将问题分为以下几种方法:
instance Show mostrarTaula where
show (Taula sep []) = ""
show (Taula sep (k:lls) ) = show ([tractar sep k]) ++ "\n"
++ show (mostrarTaula (Taula sep lls))
--["Hola","Que","fas"] --> "HolakkQuekkfas"
tractar :: String -> [String] -> String
tractar sep [] = ""
tractar sep par = (head par) ++ sep ++ tractar sep (drop 1 par)
mostrarTaula :: Taula -> [String] --[[String]] = lls i k=[String]
mostrarTaula (Taula sep []) = [""]
mostrarTaula (Taula sep (k:lls) ) = [tractar sep k] ++ (mostrarTaula(Taula sep lls))
我能做什么?我尝试打印由[(String,String,String)]组成的数据类型?我想以表格形式显示数据。每个元组后都有\ n。
输出示例:
first element tuple1 Second element tuple1 third element tuple1
first element tuple2 Second element tuple2 third element tuple2
first element tuple3 Second element tuple3 third element tuple3
答案 0 :(得分:1)
我不完全明白什么是想要的输出,但也许你可以适应这个:
instance Show Taula where
show (Taula sep []) = ""
show (Taula sep (k:lls)) = tractar sep k ++ "\n" ++ show (Taula sep lls)