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 ...
随机推荐
- typescript静态属性,静态方法,抽象类,多态
/* 1.vscode配置自动编译 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": "./js", 2.第二步 任务 ...
- FIN omitted, FIN-ACK sent
STACKOVER ADDRESS:https://stackoverflow.com/questions/21390479/fin-omitted-fin-ack-sent question: As ...
- 排序算法的python实现
几个排序算法 几个排序算法 冒泡排序 选择排序 插入排序 快速排序 quick sort 冒泡排序
- Linux TCP/IP调优-Linux内核参数注释
固定文件的内核参数 下列文件所在目录: /proc/sys/net/ipv4/ 名称 默认值 建议值 描述 tcpsyn_retries 5 1 对于一个新建连接,内核要发送多少个SYN连接请求才决定 ...
- WPF vs2015,vs2012 添加ArcObjects SDK
一:如果是vs2015,找到注册表中HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\下面的这两个,并且将名称和数 ...
- 安全测试7_Web安全在线工具
1.搜索引擎语法简单讲解:(实际上就是搜索引擎的高级搜索) 类似百度:可以看到下图我们是想在指定站点搜索包含login的页面,搜索语法为site:(testphp.vulnweb.com) " ...
- mysql给查询的结果添加序号
1.法一: select (@i:=@i+1) i,a.url from base_api_resources a ,(select @i:=0) t2 order by a.id de ...
- ORA-01461的解决过程~~
转自:http://blog.itpub.net/7607759/viewspace-521189 近日生产库中的一个过程报出了ora-1461的错误,虽然错误实际处理起来非常简单,但解决过程中与ya ...
- openstack provider self-service network subnet 创建
- python 插入html到数据库 re.escape() PyMysql
python 把html 网页源码插入到mysql 成功,部分汉字乱码是 mysql编码问题