Haskell实例展示

发布于 2024-11-01 09:14:06 字数 294 浏览 0 评论 0原文

嗨,我有一个 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

感谢帮助来向它们展示...

hi I have a haskell module which have this data type

data Blabla = Blabla [Integer]
[Char]
[(Integer,Char,Char,Integer,String)] Integer

I want to show them like that with using instance show

integers=[1,2,3]
chars=[a,b,c]
specialList=[(1,a,b,2,cd),(3,b,c,4,gh)]
interger=44

thanx for helping...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-11-08 09:14:06

假设您只需要默认样式,只需将 deriving Show 添加到行尾即可,如下所示。

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

Blabla 构建为命名结构可能会更好。

 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

Assuming you just want the default style, simply adding deriving Show to the end of the line as below should do the job.

data Blabla = Blabla [Integer] [Char] [(Integer,Char,Char,Integer,String)] Integer deriving Show

Will work fine as all of the primitive types that Blabla is built from are "showable". For example

*Main> Blabla [1,2,3] "abc" [(1,'A','B',2,"Three")] 54
Blabla [1,2,3] "abc" [(1,'A','B',2,"Three")] 54

It might be better to build Blabla as a named structure

 data BlaBlu = BlaBlu {
    theNumbers :: [Integer] ,
    theIdentifier :: [Char] ,
    theList :: [(Integer,Char,Char,Integer,String)]  ,
    theInteger :: Integer
 } deriving Show

By doing this you might be able to make the structure make more sense.

*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}

Do the same thing for the list structure and hopefully the code will be more readable.

If you want to write your own instance of Show so you can customize it then you can remove the deriving Show and just write your own instance, such as:

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              

Where the implementation produces roughly the output you asked in the original question.

*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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文