Monad

inc n = Just (n + 1)

add1 n = [n + 1]

main = do
print $ Nothing >> (Just 0) -- Nothing
print $ (Just 0) >> (Nothing :: Maybe Int) -- Nothing
print $ (Just 0) >> Nothing >> (Just 1) -- Nothing
print $ (Just 0) >> (Just 1) >> (Just 2) -- Just 2 print $ Nothing >>= inc >>= inc >>= inc -- Nothing
print $ (Just 0) >>= inc >>= inc >>= inc -- Just 3 print $ [] >> [1, 2] -- []
print $ [1, 2] >> ([] :: [Int]) -- []
print $ [1] >> [3, 4, 5] -- [3,4,5]
print $ [1, 2] >> [3, 4, 5] -- [3,4,5,3,4,5]
print $ [1, 2, 3] >> [3, 4, 5] -- [3,4,5,3,4,5,3,4,5] print $ [] >>= add1 >>= add1 >>= add1 -- []
print $ [1, 2, 3] >>= add1 -- [2,3,4]
print $ [1, 2, 3] >>= add1 >>= add1 -- [3,4,5]
print $ [1, 2, 3] >>= add1 >>= add1 >>= add1 -- [4,5,6]

根据 Monad Maybe 的定义、

instance  Monad Maybe  where
(Just x) >>= k = k x
Nothing >>= _ = Nothing Just _m1 >> m2 = m2
Nothing >> _m2 = Nothing

可得

Nothing >>= inc = Nothing
(Just 0) >>= inc
= (Just 0) >>= (\n -> Just (n + 1))
= (\n -> Just (n + 1)) 0
= Just (0 + 1) = Just 1 Nothing >> (Just 0) = Nothing
(Just 0) >> (Nothing :: Maybe Int) = Nothing
(Just 1) >> (Just 2) = Just 2

根据 Monad [] 的定义、

instance Monad []  where
xs >>= f = [y | x <- xs, y <- f x]
xs >> ys = [y | _ <- xs, y <- ys]

可得

[1, 2, 3] >>= add1
= [1, 2, 3] >>= (\n -> [n + 1])
= [y | x <- [1, 2, 3], y <- [x + 1]]
= [2,3,4]
[1, 2, 3] >> [3, 4, 5]
= [y | _ <- [1, 2, 3], y <- [3, 4, 5]]
= [3,4,5,3,4,5,3,4,5]

State Monad

import Control.Monad.State

inc :: State Int Int
inc = do
n <- get
put (n + 1)
return n incBy :: Int -> State Int Int
incBy x = do
n <- get
modify (+x)
return n main = do
print $ evalState inc 1 -- 1
print $ execState inc 1 -- 2
print $ runState inc 1 -- (1,2)
print $ runState (withState (+3) inc) 1 -- (4,5)
print $ runState (mapState (\(a, s) -> (a + 3, s + 4)) inc) 1 -- (4,6) print $ runState (incBy 5) 10 -- (10,15)

关于 State Monad

get 将结果值设置为状态值 s,状态值 s 保持不变。

put s 将结果值设为空,将状态值设为 s。

return a 将结果值设为 a,状态值 s 保持不变。

modify f 将结果值设为空,将状态值设为 f s。

gets f 将结果值设为 f s,状态值 s 保持不变。

由此可得

--  假设初始状态值为 s
inc = do
n <- get -- (s,s)
put (n + 1) -- ((),s + 1)
return n -- (s,s + 1)
-- 假设初始状态值为 s
incBy x = do
n <- get -- (s,s)
modify (+x) -- ((),s + x)
return n -- (s,s + x)

关于 State Monad

evalState s 针对 State Monad 利用初始状态值 s 进行状态计算,然后返回最终结果值 a’。

execState s 针对 State Monad 利用初始状态值 s 进行状态计算,然后返回最终状态值 s’。

runState s 针对 State Monad 利用初始状态值 s 进行状态计算,然后返回最终结果值和最终状态值组成的一对值 (a’, s')。

mapState f 针对 State Monad 进行状态计算之后,对最终结果值和状态值调用函数 f。

withState f 针对 State Monad 进行状态计算之前,对初始状态值调用函数 f。

由此可得

runState inc 1 = (1,1 + 1) = (1,2)
evalState inc 1 = 1
execState inc 1 = 2
runState (incBy 5) 10 = (10,10 + 5) = (10,15)
runState (withState (+3) inc) 1
= runState inc ((+3) 1)
= runState inc 4
= (4,5)
runState (mapState (\(a, s) -> (a + 3, s + 4)) inc) 1
= (\(a, s) -> (a + 3, s + 4)) (runState inc 1)
= (\(a, s) -> (a + 3, s + 4)) (1,2)
= (4,6)

Reader Monad

import Control.Monad.Reader

data Environment = Environment { text1 :: String, text2 :: String }

getText :: Reader Environment String
getText = do
t1 <- asks text1 -- Hello
t2 <- asks text2 -- world!
t3 <- withReader text1 ask -- Hello
t4 <- mapReader text2 ask -- world!
return $ t1 ++ ", " ++ t2 ++ ", " ++ t3 ++ ", " ++ t4 main = print $ runReader getText $ Environment "Hello" "world!"

Writer Monad

import Control.Monad.Writer

write :: Int -> Writer [Int] String
write n = do
tell [1..n]
return "Done" main = do
print $ runWriter $ write 10 -- ("Done",[1,2,3,4,5,6,7,8,9,10])
print $ execWriter $ write 10 -- [1,2,3,4,5,6,7,8,9,10]

Haskell语言练习的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言为什么值得你去学习

    摘自http://www.vaikan.com/why-haskell-is-worth-learning/ Haskell语言为什么值得你去学习 当我向一些新手推荐学习Haskell语言时,得到的反 ...

  7. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

  8. Haskell语言学习笔记(44)Lens(2)

    自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...

  9. Haskell语言学习笔记(38)Lens(1)

    Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...

  10. Haskell语言学习笔记(92)HXT

    HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...

随机推荐

  1. 关于把Json数据绑定到select2中

    最近做的一个项目中用到select2,想把Json的数据绑定到select2中,select2默认的能够接受的json格式的数据是以{id:"",text:''}这样的键值对来保存 ...

  2. 转 WiFi的STA和AP模式指什么?

    1):AP,也就是无线接入点,是一个无线网络的创建者,是网络的中心节点.一般家庭或办公室使用的无线路由器就一个AP. 2):STA站点,每一个连接到无线网络中的终端(如笔记本电脑.PDA及其它可以联网 ...

  3. SQL SERVER 数据库字段简单加密解密

    --------加密函数----------- )) RETURNS varbinary(max) AS BEGIN declare @pwd varbinary(max) SELECT @pwd = ...

  4. 01.File文件基本操作

    1-创建File对象 /** * 创建我们 java.io.File对象 */ public static void test1() { //第一创建对象方式 File parent=new File ...

  5. 04 bash程序的基本特性

    我们知道Linux系统有两种操作方式,一种为GUI的图形界面化管理方式,其中图形程序常见的有Gnome.KDE.xfce:另一种管理方式就是就是GLI的命令行管理方式,而命令行的管理方式就是通过she ...

  6. HDFS的操作SHELL和API

    HDFS的shell操作和JavaAPI的使用: WEB WEB端口50090查看SecondaryNameNode信息.可以查看Hadoop的版本,NameNode的IP,Checkpoint等信息 ...

  7. 《Linux 性能及调优指南》2.4 基准工具

    翻译:飞哥 (http://hi.baidu.com/imlidapeng) 版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明. 原文名称:<Linux Performance a ...

  8. 各平台免费翻译API

    google http://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&s ...

  9. Solr——Windows下部署Solr7.5.0至jetty、Tomcat

    Solr6用默认的Jetty启动 需要的软件支持 JDK1.8以上 Solr6的安装包(solr-7.5.0.zip ) Solr自带一个Jetty环境,可以很方便的运行Solr. 直接到解压缩的so ...

  10. Echarts动态加载饼状图实例(二)

    一.引入echarts.js文件(下载页:http://echarts.baidu.com/download.html) 二.HTML代码: <div class="ui-contai ...