haskell基本语法
定义新类型
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基本语法的更多相关文章
- Haskell学习-函数式编程初探
原文地址:Haskell学习-函数式编程初探 为什么要学习函数式编程?为什么要学习Haskell? .net到前端,C#和JavaScript对我来说如果谈不上精通,最起码也算是到了非常熟悉的 ...
- haskell学习资料
Haskell基础语法 Real World Haskell 中文版 Haskell趣学指南
- 体验一把haskell
这几天做到PAT一道比较数据大小的题PAT1065,题目不难,应该说是一道送分题,就是开数组,然后模拟人工计算的过程进行计算,再比较下就行.做完之后,联想到haskell的Integer类型是无限大的 ...
- Google 开源技术protobuf
http://blog.csdn.net/hguisu/article/details/20721109#0-tsina-1-1601-397232819ff9a47a7b7e80a40613cfe1 ...
- 无责任比较thrift vs protocol buffers
http://blog.csdn.net/socoolfj/article/details/3855007 最新版本的Hadoop代码中已经默认了Protocol buffer作为RPC的默认实现,原 ...
- erlang虚拟机代码执行原理
转载:http://blog.csdn.NET/mycwq/article/details/45653897 erlang是开源的,很多人都研究过源代码.但是,从erlang代码到c代码,这是个不小 ...
- protobuf和thrift对比
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt383 数据类型 protobuf thrift protobuf thrif ...
- 自己使用过比较好用的VSCode插件
C/C++ [ms-vscode.cpptolls] 智能推导,调试和代码浏览 C/C++ Clang Command Adapter [mitaki28.vscode-clang] 使用 ...
- erlang虚拟机代码运行原理
erlang是开源的,非常多人都研究过源码.可是.从erlang代码到c代码.这是个不小的跨度.并且代码也比較复杂. 所以这里,我利用一些时间,整理下erlang代码的运行过程.从erlang代码编译 ...
随机推荐
- scite配置文件及常用设置
在linux系统中,SciTE的用户设置文件为 ~/.SciTEUser.properties,优先级高于全局配置文件. scite是个不错的IDE工具,只是本人发现,在开发团队中和其他成员的编辑工具 ...
- go 学习之io/ioutil包
// Discard 是一个 io.Writer 接口,调用它的 Write 方法将不做任何事情// 并且始终成功返回.var Discard io.Writer = devNull(0) // Re ...
- 73.Largest Rectangle in Histogram(最大矩形)
Level: Hard 题目描述: Given n non-negative integers representing the histogram's bar height where the ...
- XSS——跨站脚本攻击
跨站点脚本攻击:通过对网页注入恶意脚本,成功地被浏览器执行,来达到攻击的目的. 一.XSS攻击类型与原理1. 反射型XSS攻击非持久性攻击,黑客使用社交性的交互技巧诱导用户点击访问目标服务器的链接,但 ...
- JavaScript中的方法和属性
书读百遍其义自见 学习<JavaScript设计模式>一书时,前两个章节中的讲解的JavaScript基础知识,让我对属性和方法有了清晰的认识.如下是我的心得体会以及部分摘录的代码. 不同 ...
- sq - 压缩一个排过序的单词列表 unsq - 解压一个排过序的单词列表
总览 (SYNOPSIS) sq < infile > outfile unsq < infile > outfile 描述 (DESCRIPTION) sq 压缩 一个 排过 ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
- ivew select组件 DatePicker组件的清空
<Form ref="formInline" :model="formInline" :rules="ruleInline" inli ...
- java ajax返回 Json 的 几种方式
原文:https://blog.csdn.net/qq_26289533/article/details/78749057 方式 1. : 自写代码转 Json 需要 HttpHttpServlet ...
- dotnet 跨平台编译发布
dotnet publish 命令,bash脚本如下(Windows安装git即可建议sh关联) publish.sh #!/usr/bin/env bash # one line command: ...