haskell笔记(1)- data type

data 定义

we can use the same name for data type and value constructor, This has no special meaning, like Person in the following:

data Point = Point Float Float deriving(Show)

Value constructors are actually **functions that ultimately return a value of a data type. **

record syntax

当一种data type包含很多字段的时候,用旧的方式定义会完全搞不懂什么是什么,recoard syntax省事多了
并且这样会声称字段对应的方法 model, year

>> data Car = Car { model :: String, year :: int } deriving Show
>> car = Car { model = "Honda", year = 1988}
>> model car
Honda
>> car 
Car { model = "Honda", year = 1988}

type parameter and type constructor

给data type加参数可以生成不同的data type,Maybe可以生成Maybe [Char] Maybe Int之类

data Maybe a = Nothing | Just a
-- a 只是定义了类型, 跟参数完全没关系
-- ZipList Int之类的是个整体,而不是真的有具体的值ZipList 1
data ZipList a = ZipList { getZipList :: [a] } 

newtype

newtypedata高效,因为data要重新包装,而newtype只包含一个值,只是值的类型被重新定义了一下

data ZipList a = ZipList { ZipList :: [a]}
newtype ZipList a = ZipList { ZipList :: [a]}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容