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
感谢帮助来向它们展示...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您只需要默认样式,只需将
deriving Show
添加到行尾即可,如下所示。会工作得很好,因为 Blabla 构建的所有原始类型都是“可显示的”。例如,
将
Blabla
构建为命名结构可能会更好。通过这样做,您可能能够使结构更有意义。
对列表结构做同样的事情,希望代码会更具可读性。
如果您想编写自己的
Show
实例,以便可以自定义它,那么您可以删除deriving Show
并只编写您自己的实例,例如:实现产生的位置大致是您在原始问题中询问的输出。
Assuming you just want the default style, simply adding
deriving Show
to the end of the line as below should do the job.Will work fine as all of the primitive types that Blabla is built from are "showable". For example
It might be better to build
Blabla
as a named structureBy doing this you might be able to make the structure make more sense.
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 thederiving Show
and just write your own instance, such as:Where the implementation produces roughly the output you asked in the original question.