Haskell语言练习
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语言练习的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言为什么值得你去学习
摘自http://www.vaikan.com/why-haskell-is-worth-learning/ Haskell语言为什么值得你去学习 当我向一些新手推荐学习Haskell语言时,得到的反 ...
- Haskell语言学习笔记(72)Free Monad
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...
- Haskell语言学习笔记(44)Lens(2)
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...
- Haskell语言学习笔记(38)Lens(1)
Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...
- Haskell语言学习笔记(92)HXT
HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...
随机推荐
- 第一章 :zabbix监控
1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源 网站/服务器 的可用性 1.1.1 网站可用性 在软件系统的高可靠性(也称为可用性,英文描述为HA ...
- 小程序API录音 微信录音后 Silk格式转码MP3
http://www.cnblogs.com/wqh17/p/6911748.html
- C# DataGridView导出Excel
using Microsoft.Office.Interop.Excel; using Excel=Microsoft.Office.Interop.Excel; //这 ...
- Ubuntu 16.04.3 LTS 部署 Cloud Torrent
下载安装程序 apt install -y curl sudo curl https://i.jpillora.com/cloud-torrent! | bash 写配置文件 sudo vim /et ...
- 由web项目中上传图片所引出的路径问题
我在做javaweb项目的时候,有个项目中需要进行图片的上传,有次我重新部署项目后,发现之前上传的图片不见了,最后找出原因:图片上传在服务器目录上,而不是绝对路径,所以特别想弄清楚javaweb项目中 ...
- react学习笔记(一)
React的介绍: React来自于Facebook公司的开源项目 React 可以开发单页面应用 spa(单页面应用) react 组件化模块化 开发模式 React通过对DOM的模拟(虚拟dom) ...
- Java 2-Java 对象和类
Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 消息解析 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例,有状态和行为. ...
- [SQL Server]无法创建 SSIS 运行时对象,请验证 DTS.dll 是否可用及是否已注册
很大可能是SQL Server Management Studio(SSMS)版本与当前操作系统不兼容造成的,与数据库本身没有关系,这种情况基本无解,不过可以使用其他机器连本机数据库导入导出数据. 今 ...
- 常用正则表达式—邮箱(Email)
本文针对有一点正则基础的同学,如果你对正则一无所知,请移步“正则表达式30分钟入门教程”学习. 要验证一个字符串是否为邮箱的话,首先要了解邮箱账号的格式.我尝试过在网上找出一个标准的格式,但是很遗憾 ...
- 阅读别人的程序(Java篇)
1.递归求数组最大值 2.转账(事务) 1.递归应用 public class DiGuiDemo3 { public static void main(String[] args) { int[] ...