定义新类型

data EmployeeInfo = Employee Int String String [String]
deriving(Read, Show, Eq, Ord)

  

EmployeeInfo是新的类型名称,或者说是类型标识

Employee是构造函数的名称

*Main> :t Employee
Employee :: Int -> String -> String -> [String] -> EmployeeInfo

  

这个函数就是将输入的各个参数转换为一个EmployeeInfo的对象。

除此之外,deriving列举了EmployeeInfo支持的操作,比如show,read, 比较相等,以及比较大小的操作。

*Main> :t read
read :: Read a => String -> a
*Main> let thisGuy = read "Employee 12345 \"Daniel King\" \"Boss X\" [\"Colleague A\",\"Colleague B\"]"::EmployeeInfo
*Main> :t thisGuy
thisGuy :: EmployeeInfo
*Main> thisGuy
Employee 12345 "Daniel King" "Boss X" ["Colleague A","Colleague B"]

  

如果只有构造函数,而没有参数,那么与enum的作用是相同的。

或者可以认为“函数”与“值”是相同的概念。

Prelude> data Init = FullInit | PartInit deriving (Show, Read, Eq)
Prelude> :t FullInit
FullInit :: Init
Prelude> :t PartInit
PartInit :: Init
Prelude> let a = PartInit
Prelude> :t a
a :: Init
Prelude> let b = FullInit
Prelude> a == b
False
Prelude> a == PartInit
True
Prelude>

  

Eq
Equality operators == and /=
Ord
Comparison operators < <= > >=; min, max, and compare.
Enum
For enumerations only. Allows the use of list syntax such as [Blue .. Green].
Bounded
Also for enumerations, but can also be used on types that have only one constructor. Provides minBound and maxBound as the lowest and highest values that the type can take.
Show
Defines the function show, which converts a value into a string, and other related functions.
Read
Defines the function read, which parses a string into a value of the type, and other related functions.

  

参考:http://en.wikibooks.org/wiki/Haskell/Classes_and_types

Prelude Control.Monad> :t return
return :: Monad m => a -> m a
Prelude Control.Monad> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b

  

A monad is a datatype that has two operations: >>= (aka bind) and return (aka unit). return takes an arbitrary value and creates an instance of the monad with it. >>= takes an instance of the monad and maps a function over it. (You can see already that a monad is a strange kind of datatype, since in most programming languages you couldn't write a function that takes an arbitrary value and creates a type from it. Monads use a kind of parametric polymorphism .)

In Haskell notation, the monad interface is written

class Monad m where
return :: a -> m a
(>>=) :: forall a b . m a -> (a -> m b) -> m b
These operations are supposed to obey certain "laws", but that's not terrifically important: the "laws" just codify the way sensible implementations of the operations ought to behave (basically, that >>= and return ought to agree about how values get transformed into monad instances and that >>= is associative). Monads are not just about state and IO: they abstract a common pattern of computation that includes working with state, IO, exceptions, and non-determinism. Probably the simplest monads to understand are lists and option types: instance Monad [ ] where
[] >>= k = []
(x:xs) >>= k = k x ++ (xs >>= k)
return x = [x] instance Monad Maybe where
Just x >>= k = k x
Nothing >>= k = Nothing
return x = Just x
where [] and : are the list constructors, ++ is the concatenation operator, and Just and Nothing are the Maybe constructors. Both of these monads encapsulate common and useful patterns of computation on their respective data types (note that neither has anything to do with side effects or IO). You really have to play around writing some non-trivial Haskell code to appreciate what monads are about and why they are useful.

  

haskell基本语法的更多相关文章

  1. Haskell学习-函数式编程初探

    原文地址:Haskell学习-函数式编程初探   为什么要学习函数式编程?为什么要学习Haskell?   .net到前端,C#和JavaScript对我来说如果谈不上精通,最起码也算是到了非常熟悉的 ...

  2. haskell学习资料

    Haskell基础语法 Real World Haskell 中文版 Haskell趣学指南

  3. 体验一把haskell

    这几天做到PAT一道比较数据大小的题PAT1065,题目不难,应该说是一道送分题,就是开数组,然后模拟人工计算的过程进行计算,再比较下就行.做完之后,联想到haskell的Integer类型是无限大的 ...

  4. Google 开源技术protobuf

    http://blog.csdn.net/hguisu/article/details/20721109#0-tsina-1-1601-397232819ff9a47a7b7e80a40613cfe1 ...

  5. 无责任比较thrift vs protocol buffers

    http://blog.csdn.net/socoolfj/article/details/3855007 最新版本的Hadoop代码中已经默认了Protocol buffer作为RPC的默认实现,原 ...

  6. erlang虚拟机代码执行原理

     转载:http://blog.csdn.NET/mycwq/article/details/45653897 erlang是开源的,很多人都研究过源代码.但是,从erlang代码到c代码,这是个不小 ...

  7. protobuf和thrift对比

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt383 数据类型 protobuf thrift protobuf thrif ...

  8. 自己使用过比较好用的VSCode插件

    C/C++  [ms-vscode.cpptolls]    智能推导,调试和代码浏览 C/C++ Clang Command Adapter [mitaki28.vscode-clang]   使用 ...

  9. erlang虚拟机代码运行原理

    erlang是开源的,非常多人都研究过源码.可是.从erlang代码到c代码.这是个不小的跨度.并且代码也比較复杂. 所以这里,我利用一些时间,整理下erlang代码的运行过程.从erlang代码编译 ...

随机推荐

  1. UVAlive 6763 Modified LCS

    LCS stands for longest common subsequence, and it is a well known problem. A sequence in thisproblem ...

  2. 克隆虚拟机(centos7)

    当我们做分布式测试时,需要多个节点(虚拟机),除了一个个虚拟机重新安装外,还可以从一个虚拟机镜像克隆出新的虚拟机 本例中要从名为master1的虚拟机克隆一个名为node1的 输入新的虚拟机名称和文件 ...

  3. 开源企业IM-免费企业即时通讯-ENTBOOST V2014.183 Linux版本号正式公布

    版权声明:本文为博主原创文章,欢迎转载,转载请尽量保持原文章完整,谢谢! https://blog.csdn.net/yanghz/article/details/37807975 ENTBOOST, ...

  4. struts2的action方法匹配以及通配符的使用

    1. ActionMethod:Action执行的时候并不一定要执行execute方法,可以在配置文件中配置action的时候用"method"属性来指定执行哪个方法,也可以在ur ...

  5. 安卓构架组件——概述 Android Architecture Components

    谷歌官文文档地址:https://developer.android.google.cn/topic/libraries/architecture 安卓构架组建是库的集合:帮助你设计健壮的.易测试的. ...

  6. 【转载】windows 开启 nginx 监听80 端口 以及 禁用 http 服务后,无法重启 HTTP 服务,提示 系统错误 123,文件目录、卷标出错

    https://www.cnblogs.com/TianyuSu/p/9509873.html location / { proxy_set_header Host $Host; proxy_set_ ...

  7. ipv6地址在URL中的表达方式,如何在URL地址中包含ipv6地址

    摘要 本文档定义了在WWW浏览器的URL中执行的文本IPv6地址的格式.在包括Microsoft的IE,Mozilla和Lynx等几个已经被广泛安装使用的浏览器的IPv6版本中,这种格式已经被使用.并 ...

  8. 07.Linux-CentOS系统库文件libaudit.so.1丢失问题

    问题:缺少共享库文件sudo: error while loading shared libraries: libaudit.so.1: cannot open shared object file: ...

  9. Codecraft-17 and Codeforces Round #391 - B

    题目链接:http://codeforces.com/contest/757/problem/B 题意:给定n个数字,问最多能选个多少个数字使得选出来的数字的gcd!=1. 思路:由于数字最大为1e5 ...

  10. 解释ARP协议和RARP协议

    解释ARP(地址解析协议) 首先,每个主机都会在自己的ARP缓冲区中建立一个ARP列表,以表示IP地址和MAC地址之间的对应关系. 当源主机要发送数据时,首先检查ARP列表中是否有对应IP地址的目的主 ...