定义新类型

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. mysql 主从复制 (2)

    今天说一下MySQL的主从复制如何做到! 准备工作: 1.两个虚拟机:我这里用的是CentOS5.5,IP地址分别是192.168.1.101 和192.168.1.105: 101做主服务器,105 ...

  2. Libre OJ 2255 (线段树优化建图+Tarjan缩点+DP)

    题面 传送门 分析 主体思路:若x能引爆y,从x向y连一条有向边,最后的答案就是从x出发能够到达的点的个数 首先我们发现一个炸弹可以波及到的范围一定是坐标轴上的一段连续区间 我们可以用二分查找求出炸弹 ...

  3. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  4. 在vue中设计一个客户签名的功能

    直接贴代码: <template> <div class="hello"> <p>签字:</p> <canvas id=&qu ...

  5. Source Insight symbol not found

    使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...

  6. CSS-05 html和body标签

    html和body标签 一直对这两个标签有迷惑,查了一些网上资料整理了一下. 1.html和body标签的背景 1.当给body一个背景色时候,背景图是充满整个窗口的,这里看上去是body标签下的背景 ...

  7. 关于在IE下JavaScript的 Stack overflow at line 错误可能的原因

    该错误只在IE中出现,出现该提示的原因主要有两种: 1. 重定义了系统的触发事件名称作为自定义函数名如:  onclick / onsubmit …  都是系统保留的事件名称,不允许作为重定义函数名称 ...

  8. 一、RequireHttps

    一.RequireHttps 强制使用Https重新发送请求:如: public class HomeController : Controller { [RequireHttps] public A ...

  9. Codeforces Round #394 (Div. 2) - C

    题目链接:http://codeforces.com/contest/761/problem/C 题意:给定n个长度为m的字符串.每个字符串(字符串下标从0到m-1)都有一个指针,初始指针指向第0个位 ...

  10. postgresql绿色版安装及Navicat创建数据库,导入导出sql

    转载:https://www.cnblogs.com/winkey4986/p/5360551.html 1.设置安装路径为:D:\soft\pgsql,数据存储路径为:D:\soft\pgsql\d ...