Haskell语言学习笔记(29)CPS
CPS (Continuation Passing Style)
CPS(延续传递风格)是指函数不把处理结果作为返回值返回而是把处理结果传递给下一个函数的编码风格。
与此相对,函数把处理结果作为返回值返回的编码风格被称为直接编码风格。
add :: Int -> Int -> Int
add x y = x + y
square :: Int -> Int
square x = x * x
pythagoras :: Int -> Int -> Int
pythagoras x y = add (square x) (square y)
add_cps :: Int -> Int -> ((Int -> r) -> r)
add_cps x y = \k -> k (add x y)
square_cps :: Int -> ((Int -> r) -> r)
square_cps x = \k -> k (square x)
pythagoras_cps :: Int -> Int -> ((Int -> r) -> r)
pythagoras_cps x y = \k ->
square_cps x $ \x_squared ->
square_cps y $ \y_squared ->
add_cps x_squared y_squared $ k
chainCPS :: ((a -> r) -> r) -> (a -> ((b -> r) -> r)) -> ((b -> r) -> r)
chainCPS s f = \k -> s $ \x -> f x $ k
pythagoras_cps' :: Int -> Int -> ((Int -> r) -> r)
pythagoras_cps' x y =
square_cps x `chainCPS` \x_squared ->
square_cps y `chainCPS` \y_squared ->
add_cps x_squared y_squared
main = do
print $ pythagoras 3 4
pythagoras_cps 3 4 print
pythagoras_cps' 3 4 print
{-
25
25
25
-}
这里 add, square, pythagoras 函数把处理结果直接返回,是直接编码风格。
而 add_cps, square_cps, pythagoras_cps 函数把处理结果传递给下一个函数,是CPS编码风格。
chainCPS 函数实现了一个通用的CPS风格函数,它能将CPS编码风格的函数串联起来。
Samples
module Continuation where
idCPS :: a -> (a -> r) -> r
idCPS a ret = ret a
mysqrt :: Floating a => a -> a
mysqrt x = sqrt x
test1 :: IO ()
test1 = print (mysqrt 4)
mysqrtCPS :: Floating a => a -> (a -> r) -> r
mysqrtCPS a k = k (sqrt a)
test2 :: IO ()
test2 = mysqrtCPS 4 print
test3 :: Floating a => a
test3 = mysqrt 4 + 2
test4 :: Floating a => a
test4= mysqrtCPS 4 (+ 2)
fac :: Integral a => a -> a
fac 0 = 1
fac n = n * fac (n - 1)
test5 :: Integral a => a
test5 = fac 4 + 2
facCPS :: Integral a => a -> (a -> r) -> r
facCPS 0 k = k 1
facCPS n k = facCPS (n - 1) $ \ret -> k (n * ret)
test6 :: Integral a => a
test6 = facCPS 4 (+ 2)
test7 :: IO ()
test7 = facCPS 4 print
newSentence :: Char -> Bool
newSentence = flip elem ".?!"
newSentenceCPS :: Char -> (Bool -> r) -> r
newSentenceCPS c k = k (elem c ".?!")
mylength :: [a] -> Integer
mylength [] = 0
mylength (_ : as) = succ (mylength as)
mylengthCPS :: [a] -> (Integer -> r) -> r
mylengthCPS [] k = k 0
mylengthCPS (_ : as) k = mylengthCPS as (k . succ)
test8 :: Integer
test8 = mylengthCPS [1..2006] id
test9 :: IO ()
test9 = mylengthCPS [1..2006] print
main = do
test1
test2
print test3
print test4
print test5
print test6
test7
print test8
test9
{-
2.0
2.0
4.0
4.0
26
26
24
2006
2006
-}
Haskell语言学习笔记(29)CPS的更多相关文章
- 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语言学习笔记(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语言学习笔记(30)MonadCont, Cont, ContT
MonadCont 类型类 class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a in ...
随机推荐
- IntelliJ IDEA常用设置
IntelliJ IDEA进入设置界面. “File”->“Settings”,进入如下界面: 界面主题设置 CTR+鼠标滚动键改变编辑区字体大小.设置鼠标在系统类上指定时间显示注释. 设 ...
- linux 查看文件夹大小 du -h --max-depth=1 ./
du:查询文件或文件夹的磁盘使用空间 如果当前目录下文件和文件夹很多,使用不带参数du的命令,可以循环列出所有文件和文件夹所使用的空间.这对查看究竟是那个地方过大是不利的,所以得指定深入目录的层数,参 ...
- 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码
概述 spring MVC框架controller间跳转,需重定向,主要有如下三种: 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedi ...
- bzoj4161: Shlw loves matrixI
Description 给定数列 {hn}前k项,其后每一项满足 hn = a1*h(n-1) + a2*h(n-2) + ... + ak*h(n-k) 其中 a1,a2...ak 为给定数列.请计 ...
- bzoj2763 飞行路线
Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...
- js在html文件中的解析顺序
我们可以将JavaScript代码放在html文件中任何位置,但是我们一般放在网页的head或者body部分. 放在<head>部分 最常用的方式是在页面中head部分放置<scri ...
- Air test ios类使用
1.iOS手机的滑动 相关代码 #python class IOS(Device): ... @property #获取屏幕的尺寸 def display_info(self): if not sel ...
- 阳虚体质外感/胃脘痛/经期抽搐x案
* 咽干咽痛 某女 42岁 在40岁产下一子,后体质明显不如以前,几年以来,易感冒,咳嗽 每次在社区医院输液,少则一个月,多则几个月方能愈,几天前外感微咳,咽痛声嘶 观其咽并不红,舌淡苔薄白 双手 ...
- 现在的 Linux 内核和 Linux 2.6 的内核有多大区别?
作者:larmbr宇链接:https://www.zhihu.com/question/35484429/answer/62964898来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- 清理mysql binlog日志
1.查看binlog日志 mysql> show binary logs; +------------------+------------+| Log_name | File_ ...