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代码编译 ...
随机推荐
- Mybatis开发中前端控制器注解@Controller 引用的类错误
import org.springframework.web.portlet.ModelAndView; 错误 import org.springframework.web.servlet.Model ...
- luoguP1600 天天爱跑步(NOIP2016)(主席树+树链剖分)
阅读体验: https://zybuluo.com/Junlier/note/1303550 为什么这一篇的Markdown炸了? # 天天爱跑步题解(Noip2016)(桶+树上差分 ^ 树剖+主席 ...
- 【记录】使用Navicat将表设计导出数据库设计文档
INFORMATION_SCHEMA. Tables -- 表信息 INFORMATION_SCHEMA. COLUMNS -- 列信息 参考文章地址:https://blog.csdn.net/cx ...
- IDS4授权分析
一.授权过程的场景名词 RO (resource owner): 资源所有者:用户. Resource Owner:简单理解为资源拥有者,文中称“user”: RS (resource server) ...
- [SCOI2003]字符串折叠(区间dp)
P4302 [SCOI2003]字符串折叠 题目描述 折叠的定义如下: 一个字符串可以看成它自身的折叠.记作S = S X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) = SSSS ...
- maven 配置自动本地/线上不同配置自动打包
工程结构:在resource下新建开发,线上不同文件夹存放不同配置文件 pom.xml配置文件 <!-- maven配置不同环境打包 --> <build> <plugi ...
- JavaScript实例之计算器
代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- python-字符串的处理
s1 = '###12314##231###' print(s1.split('#')) #split,从左往右遇见# 就拆分一次['', '', '', '12314', '', '231', '' ...
- 太可怕了!黑客是如何攻击劫持安卓用户的DNS?
最近发现的针对Android设备的广泛路由器的DNS劫持恶意软件现在已升级为针对iOS设备以及桌面用户的功能. 被称为RoamingMantis的恶意软件最初发现在上个月劫持了互联网路由器,以散布旨在 ...
- zookeeper,通过python连接找出其对应的hive
#通过zookpeer找到对应的hive from kazoo.client import KazooClient zkClient = KazooClient(hosts="ip1:218 ...