Haskell实例显示

| 嗨,我有一个具有此数据类型的haskell模块
data Blabla = Blabla [Integer]
[Char]
[(Integer,Char,Char,Integer,String)] Integer
我想通过实例显示来展示给他们
integers=[1,2,3]
chars=[a,b,c]
specialList=[(1,a,b,2,cd),(3,b,c,4,gh)]
interger=44
thanx帮助...     
已邀请:
假设您只想要默认样式,只需在行尾添加ѭ2line即可,如下所示。
data Blabla = Blabla [Integer] [Char] [(Integer,Char,Char,Integer,String)] Integer deriving Show
由于构建Blabla的所有原始类型都是“可显示”的,因此可以正常工作。例如
*Main> Blabla [1,2,3] \"abc\" [(1,\'A\',\'B\',2,\"Three\")] 54
Blabla [1,2,3] \"abc\" [(1,\'A\',\'B\',2,\"Three\")] 54
最好将“ 5”构建为命名结构
 data BlaBlu = BlaBlu {
    theNumbers :: [Integer] ,
    theIdentifier :: [Char] ,
    theList :: [(Integer,Char,Char,Integer,String)]  ,
    theInteger :: Integer
 } deriving Show
通过这样做,您也许可以使结构更合理。
*Main> BlaBlu [1,2,3] \"abc\" [(1,\'A\',\'B\',2,\"Three\")] 54
BlaBlu {theNumbers = [1,2,3], theIdentifier = \"abc\", theList = [(1,\'A\',\'B\',2,\"Three\")], theInteger = 54}
对列表结构做同样的事情,希望代码将更具可读性。 如果要编写自己的
Show
实例,以便对其进行自定义,则可以删除
deriving Show
并仅编写自己的实例,例如:
instance Show Blabla where                                                                                       
    show (Blabla ints chars list num) =                                                                            
    \"integers = \" ++ show ints ++ \"\\n\" ++                                                                        
    \"chars = \" ++ show chars ++ \"\\n\" ++                                                                          
    \"specialList = \" ++ show list ++ \"\\n\" ++                                                                     
    \"integer = \" ++ show num              
实现在何处大致产生您在原始问题中要求的输出。
*Main> Blabla [1,2,3] \"abc\" [(1,\'A\',\'B\',2,\"Three\")] 54
integers = [1,2,3]
chars = \"abc\"
specialList = [(1,\'A\',\'B\',2,\"Three\")]
integer = 54
    

要回复问题请先登录注册