怎样理解Functor与Monad
1. 复合函数操作符
Prelude> :t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
Prelude> (.) ((+) 5) ((*) 2) 4
13
所以(.)操作符的作用,是将4作为参数传递给((*) 2)函数,再将结果传递给((+) 5)函数。
这就是数学里面的复合函数:
f(x) = 2x
g(x) = x + 5
g(f(x)) = g(2x) = (2x) + 5 = 2x + 5
g(f(4)) = 2*4 + 5 = 13
2. Functor
194 {- | The 'Functor' class is used for types that can be mapped over.
195 Instances of 'Functor' should satisfy the following laws:
196
197 > fmap id == id
198 > fmap (f . g) == fmap f . fmap g
199
200 The instances of 'Functor' for lists, 'Data.Maybe.Maybe' and 'System.IO.IO'
201 satisfy these laws.
202 -}
203
204 class Functor f where
205 fmap :: (a -> b) -> f a -> f b
206
207 -- | Replace all locations in the input with the same value.
208 -- The default definition is @'fmap' . 'const'@, but this may be
209 -- overridden with a more efficient version.
210 (<$) :: a -> f b -> f a
211 (<$) = fmap . const
id是一个函数
Prelude> :t id
id :: a -> a
Prelude> id "Daniel"
"Daniel"
Prelude> id 1
1
Prelude> id True
True
Prelude> id Just "Happy"
Just "Happy"
Prelude> id Nothing
Nothing
Functor是一种typeclass,用来定义一种允许的操作集合,这里是fmap,并且对于fmap提出了需要满足的条件:
- fmap id == id
- fmap (f . g) == fmap f . fmap g
可以视为"交换律"和“分配律”
Prelude Data.Char> fmap isDigit ((++) ['0'..'9'] ['a'..'f'])
[True,True,True,True,True,True,True,True,True,True,False,False,False,False,False,False]
Prelude Data.Char> :t isDigit
isDigit :: Char -> Bool
fmap不仅可以操作List(此时与List的函数map作用相同),还可以操作比如Maybe
Prelude Data.Char> fmap isDigit (Just 'a')
Just False
Prelude Data.Char> fmap isDigit (Nothing)
Nothing
Prelude Data.Char> fmap isDigit (Just '1')
Just True
3. Manod
Monad与Functor一样,也是用来定义类型可以进行的操作集合。
Prelude Data.Char> :i Monad
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
fail :: String -> m a
-- Defined in `GHC.Base'
instance Monad Maybe -- Defined in `Data.Maybe'
instance Monad (Either e) -- Defined in `Data.Either'
instance Monad [] -- Defined in `GHC.Base'
instance Monad IO -- Defined in `GHC.Base'
instance Monad ((->) r) -- Defined in `GHC.Base'
可以看到,这些操作包括(>>=) (>>) return fail
(>>)执行两个操作,但是放弃前一个操作的结果
Prelude Data.Char> :t (>>)
(>>) :: Monad m => m a -> m b -> m b
Prelude Data.Char> (>>) "Daniel" "King"
"KingKingKingKingKingKing"
212
213 {- | The 'Monad' class defines the basic operations over a /monad/,
214 a concept from a branch of mathematics known as /category theory/.
215 From the perspective of a Haskell programmer, however, it is best to
216 think of a monad as an /abstract datatype/ of actions.
217 Haskell's @do@ expressions provide a convenient syntax for writing
218 monadic expressions.
219
220 Minimal complete definition: '>>=' and 'return'.
221
222 Instances of 'Monad' should satisfy the following laws:
223
224 > return a >>= k == k a
225 > m >>= return == m
226 > m >>= (\x -> k x >>= h) == (m >>= k) >>= h
227
228 Instances of both 'Monad' and 'Functor' should additionally satisfy the law:
229
230 > fmap f xs == xs >>= return . f
231
232 The instances of 'Monad' for lists, 'Data.Maybe.Maybe' and 'System.IO.IO'
233 defined in the "Prelude" satisfy these laws.
234 -}
235
236 class Monad m where
237 -- | Sequentially compose two actions, passing any value produced
238 -- by the first as an argument to the second.
239 (>>=) :: forall a b. m a -> (a -> m b) -> m b
240 -- | Sequentially compose two actions, discarding any value produced
241 -- by the first, like sequencing operators (such as the semicolon)
242 -- in imperative languages.
243 (>>) :: forall a b. m a -> m b -> m b
244 -- Explicit for-alls so that we know what order to
245 -- give type arguments when desugaring
246
247 -- | Inject a value into the monadic type.
248 return :: a -> m a
249 -- | Fail with a message. This operation is not part of the
250 -- mathematical definition of a monad, but is invoked on pattern-match
251 -- failure in a @do@ expression.
252 fail :: String -> m a
253
254 {-# INLINE (>>) #-}
255 m >> k = m >>= \_ -> k
256 fail s = error s
(>>=)是将前一个操作的结果作为参数传递给后一个操作,但是注意需要使用return将从a到b的正常转换(a -> b)变成(a -> mb),即(a -> ma)(a - b) = (a -> mb)
Prelude Data.Char> :t return
return :: Monad m => a -> m a
Prelude Data.Char> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
Prelude Data.Char> (>>=) (return ((++) "Daniel" "King")) ((:) 'X')
"XDanielKing"
Prelude Data.Char> (>>=) (return ((++) "Daniel" "King")) ((++) "Hello ")
"Hello DanielKing"
所以(>>=)和return是配合使用的,效果很类似于Unix下的管道操作。
Monad的设计想法是允许pure的Haskell可以产生一些side effect,或者说除了自身的值以外,可以保存下一些状态信息。
比如在这里, ((++) "Daniel" "King")的结果就传递给了后面的action,这样就可以用来更新状态信息。
比较明显的应用是在IO以及Exception Handling上面。
如果参考数学中的概念,Monad可以被看作是虚数体系,或者是另外一个维度的类型;
普通的类型与Monad类型之间需要显式地进行转换,
return : 普通类型转换成Monad类型
<-:Monad类型转换成普通类型
IO action都是操作的Monad类型。
*RecursiveContents Control.Monad Data.Char System.FilePath> :t forM
forM :: Monad m => [a] -> (a -> m b) -> m [b]
*RecursiveContents Control.Monad Data.Char System.FilePath> forM "Daniel King" $ \ch -> do return (toUpper ch)
"DANIEL KING"
怎样理解Functor与Monad的更多相关文章
- 函数编程中functor和monad的形象解释
函数编程中functor和monad的形象解释 函数编程中Functor函子与Monad是比较难理解的概念,本文使用了形象的图片方式解释了这两个概念,容易理解与学习,分别使用Haskell和Swift ...
- 泛函编程(28)-粗俗浅解:Functor, Applicative, Monad
经过了一段时间的泛函编程讨论,始终没能实实在在的明确到底泛函编程有什么区别和特点:我是指在现实编程的情况下所谓的泛函编程到底如何特别.我们已经习惯了传统的行令式编程(imperative progra ...
- Functor and Monad in Swift
I have been trying to teach myself Functional Programming since late 2013. Many of the concepts are ...
- Monad / Functor / Applicative 浅析
前言 Swift 其实比 Objective-C 复杂很多,相对于出生于上世纪 80 年代的 Objective-C 来说,Swift 融入了大量新特性.这也使得我们学习掌握这门语言变得相对来说更加困 ...
- 重新理解 Monad
对于大多数刚刚入门函数式编程的同学来说,monad(单子.又叫单体)可能是这里面的一道坎.你可能对 map . flatMap 以及 filter 再熟悉不过,可是到了高阶的抽象层次上就又会变得一脸懵 ...
- Functor、Applicative 和 Monad(重要)
Functor.Applicative 和 Monad Posted by 雷纯锋Nov 8th, 2015 10:53 am Functor.Applicative 和 Monad 是函数式编程语言 ...
- 浅释Functor、Applicative与Monad
引言 转入Scala一段时间以来,理解Functor.Applicative和Monad等概念,一直是我感到头疼的部分.虽然读过<Functors, Applicatives, And Mona ...
- Functor、Applicative 和 Monad
Functor.Applicative 和 Monad 是函数式编程语言中三个非常重要的概念,尤其是 Monad. 说明:本文中的主要代码为 Haskell 语言,它是一门纯函数式的编程语言. 一.结 ...
- Scalaz(11)- Monad:你存在的意义
前面提到了scalaz是个函数式编程(FP)工具库.它提供了许多新的数据类型.拓展的标准类型及完整的一套typeclass来支持scala语言的函数式编程模式.我们知道:对于任何类型,我们只需要实现这 ...
随机推荐
- eclipse新建maven项目出错 pom.xml报错
问题: 1.新建项目后会提示一个这样的错 maven-compiler-plugin:3.1:compile(1 errors) maven-compiler-plugin:3.1:testCompi ...
- Linux服务正常启动,Linux服务器能访问,但是外部机器不能访问
公司用到了jenkins,就在自己虚拟机里面部署了一个jenkins.部署成功之后,在Linux虚拟机里面能正常访问,但是外部真实机却不能访问.当时的第一反应就是觉得应该是权限问题,猜测会不会是jen ...
- Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)
题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 解决嵌套在ScrollView中的TableView滑动手势冲突问题
最近在迭代开发公司项目的时候遇到了一个问题,在可以左右切换标签视图的ScrollView中嵌套了两个TableView用于展示视图,感觉一切so easy的情况下,问题出现了,因为左右两个视图既可以实 ...
- read - 在文件描述符上执行读操作
概述#include <unistd.h> ssize_t read(int fd, void *buf, size_t count); 描述read() 从文件描述符 fd 中读取 co ...
- CentOS下安装中文man 手册
为了方便使用man,安装中文手册,具体如下: 版本:CentOS release 6.6 (Final) 中文包:http://pkgs.fedoraproject.org/repo/pkgs/man ...
- H5手机端底部菜单覆盖中间部分内容的解决办法
一.第一种Js动态计算中间内容的高度. 二.第二种给底部上面写个<div style="底部的高度"></div> 三.第三种给中间部分写一个margin- ...
- EBCDIC-1025 Russia
- 编译错误:warning C4005]ws2def.h(91): warning C4005: “AF_IPX”: 宏重定义 winsock.h(460) : 参见“AF_IPX”的前一个定义
[问题] ws2def.h(91): warning C4005: “AF_IPX”: 宏重定义: winsock2.h(460) : 参见“AF_IPX”的前一个定义 [原因] windows.h头 ...
- js实现动态加载input 提示信息
思路:使用<datalist> 标签定义选项列表.请与 input 元素配合使用该元素,来定义 input 可能的值.datalist 及其选项不会被显示出来,它仅仅是合法的输入值列表.请 ...