Haskell实例展示 [英] Haskell instance show

查看:128
本文介绍了Haskell实例展示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 数据Blabla = Blabla [Integer] $ b我有一个haskell模块,它有这种数据类型$ b $ Char 
[(Integer,Char,Char,Integer,String)]整数

我想通过使用instance show来显示它们。

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

thax帮助...

解决方案

假设您只想使用默认样式,只需将派生Show 添加到该行末尾即可完成该任务。 p>

 数据Blabla = Blabla [Integer] [Char] [(Integer,Char,Char,Integer,String)]整数派生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 作为命名结构

 数据BlaBlu = BlaBlu {
theNumbers :: [Integer],
theIdentifier :: [Char],
theList :: [(Integer,Char,Char,Integer,String)],
theInteger :: Integer
}派生显示

通过这样做,您可能会使结构更有意义。

  *主要> 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 并仅编写自己的实例,例如:

  instance显示Blabla其中
显示(Blabla ints chars list num)=
integers =++显示ints ++\\\
++
chars =++ show chars ++\\\
++
specialList =++ show list ++\\\
++
integer =++ show num

执行产生的结果大概是原始问题中输出的结果。

  *主> Blabla [1,2,3]abc[(1,'A','B',2,Three)] 54 
整数= [1,2,3]
chars = abc
specialList = [(1,'A','B',2,Three)]
integer = 54


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]<br>
chars=[a,b,c]<br>
specialList=[(1,a,b,2,cd),(3,b,c,4,gh)]<br>
interger=44 <br>

thax for helping...

解决方案

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

这篇关于Haskell实例展示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆