定义新类型

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 writtenclass Monad m wherereturn :: 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 whereJust x  >>= k = k xNothing >>= k = Nothingreturn 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.

  

转载于:https://www.cnblogs.com/long123king/p/3844327.html

haskell基本语法相关推荐

  1. JavaScript、PHP、Golang、Haskell、Elixir,哪个才是最佳编程语言?

    [CSDN 编者按]哪个语言是你心中的最佳编程语言呢? 作者 | Michele Riva 译者 | 弯月    责编 | 欧阳姝黎 出品 | CSDN(ID:CSDNnews) 以下为译文: 在过去 ...

  2. Haskell大世界+思考

    文章目录 基石般灵活表现自由的抽象范式 编程语言是什么? 推荐论文 大佬建议 Meta Haskell 实现 类型系统 语言抽象/模式 问题解决方案 Haskell在工业界有哪些实际的应用? 关于fp ...

  3. 【一天一门编程语言】Haskell 语言程序设计极简教程

    Haskell 语言程序设计极简教程 一.什么是 Haskell Haskell 是一种纯函数式编程语言,它把程序设计抽象化到一个更高的层次,简化程序开发工作量,能够更快更容易地完成任务. 它是一种函 ...

  4. haskell中的Monad小记

    Haskell 中的Monad 小记 monad基本上可以解释为实现了lift,bind操作的类型类.具体解释可以参考wikipidia上的定义 Monad构成自三个部份: 类型构造子 mmm,建造一 ...

  5. Haskell 中的 Monad 和 IO

    对于 Haskell 初学者来说,Monad 和 IO 或许是掌握 Haskell 之路上的第一大难关.本文将会以尽量浅显的方式介绍 Monad 和 IO 背后的原理和设计思想,希望能够给 Haske ...

  6. protobuf和thrift对比

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

  7. 保持函数依赖的模式分解可以减轻或解决什么_为什么我更喜欢函数式编程?

    作者 | Mario Morgenthum 译者 | 平川 编辑 | 陈思 AI 前线导读:在学习 Haskell 之前,作者一直使用主流语言,如 Java.C 和 C++--现在他仍然喜欢它们.那么 ...

  8. Why Kotlin,为什么你应该选择Kotlin ?

    为什么你应该选择Kotlin ? 编程语言设计是从满足机器需求到满足程序员需求的进化途径. 我们概述了编程语言的历史发展,以便您了解Kotlin适合的位置以及为什么要学习它.这个原子介绍了一些主题,如 ...

  9. vscode新建工程

    一.在windows下新建项目(ubuntu同理) 1.在桌面新建文件夹,命名test 2.打开vsc,选择打开文件,选择test 3.将工作区另存为,还是选择test,此时会生成test.code- ...

最新文章

  1. Django进阶-auth集成认证模块
  2. Cortex-M0微处理器异常入口流程的细节
  3. Java 流程控制与数组
  4. [转]JavaScript var obj = { id:1, name:jacky } 大括号是啥意思?
  5. System.Drawing.Color转System.Windows.Media.Color
  6. mysql tomcat列表增删改查_Tomcat-Database
  7. 下班到点想走,但老员工都没动,怎么办?
  8. (进阶)LeetCode(119)——杨辉三角 II(JavaScript)
  9. mysql插入blob报错_java如何向mysql写入blob数据?
  10. 表白网页在线制作-我要表白网-最浪漫的表白网页在线生成网站
  11. 浅谈Tarjan算法
  12. CCProxy网络共享代理服务端配置使用
  13. HTML5七夕情人节表白网页制作 (蓝色主题-樱花雨3D相册)HTML+CSS+JavaScript
  14. 基于JAVA的KTV交易_Java 基于sshktv预定管理系统
  15. mysql replication 监控_MySQL之-Replication监控及自动故障切换的详细分析
  16. 5G — Overview
  17. @staticmethod静态方法
  18. 中小学教师资格考试介绍
  19. 为《理解C#中的System.In32和int:并非鸡和鸡蛋 》做个续
  20. 跟踪 Ring3 - Ring0 的运行流程

热门文章

  1. css中position:fixed为啥下面还要设置一个div的height
  2. 科研必备之SCI投稿7个阶段邮件模板,收藏!
  3. vue3 串联表格 匹配上方和左侧(个人笔记)
  4. windows10下nacos安装启动
  5. hdu 5575 Discover Water Tank(可合并堆)
  6. zabbix理论概述
  7. GitHub 上标星 9.4K 的云盘系统
  8. Jetpack Compose 从开门到入门之 MenuBar桌面菜单(Desktop Menu)
  9. 彩虹云任务墨渊邮件美化模板
  10. Can‘t convert value at index 8 to dimension: type=0x10