博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
haskell基本语法
阅读量:7068 次
发布时间:2019-06-28

本文共 3471 字,大约阅读时间需要 11 分钟。

定义新类型

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

  

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

Employee是构造函数的名称

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

  

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

 

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

 

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

  

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

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

Prelude> data Init = FullInit | PartInit deriving (Show, Read, Eq)Prelude> :t FullInitFullInit :: InitPrelude> :t PartInitPartInit :: InitPrelude> let a = PartInitPrelude> :t aa :: InitPrelude> let b = FullInitPrelude> a == bFalsePrelude> a == PartInitTruePrelude>

  

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.

  

参考:

 

Prelude Control.Monad> :t returnreturn :: Monad m => a -> m aPrelude 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 where  return :: a -> m a  (>>=) :: forall a b . m a -> (a -> m b) -> m bThese 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 xwhere [] 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

你可能感兴趣的文章
程序员如何成为架构师
查看>>
fiddler抓包之关于connect连接
查看>>
MySQL,binlog2sql回滚操作测试
查看>>
CentOS7下yum安装Jenkins
查看>>
简练软考知识点整理-确认范围管理
查看>>
不懂这几点就落后了:Android、Python工程师必读!
查看>>
Werkzeug 教程
查看>>
内核参数优化
查看>>
用户,组和权限零碎知识
查看>>
计算机
查看>>
文件修改较优方式
查看>>
oracle导入导出exp,imp
查看>>
oracle check if the display variable is set
查看>>
一键部署Openstack R版
查看>>
《JAVA——帮你解决高并发秒杀》
查看>>
国家级期刊发表要求注意事项
查看>>
C文件操作
查看>>
观察转小写的操作-字符函数
查看>>
Oracle查询访问同一表的两个以上索引(二)
查看>>
office 2016 下载地址
查看>>