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的更多相关文章

  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语言学习笔记(72)Free Monad

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

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

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

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

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

  9. Haskell语言学习笔记(30)MonadCont, Cont, ContT

    MonadCont 类型类 class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a in ...

随机推荐

  1. 分布式数据存储 shard(切片) 和 repali(副本) 的 节点数的关系。

    1 , node 的 数量 应该大于等于 副本(指的是单个 shard 的 主副本+备份副本数)的 数量 ,如果 副本的数量大于 node 数量,那么 一个node 必定有2 个相同的 副本,这个多出 ...

  2. mySQL 教程 第2章 安装和介绍mySQL

    设置mySQL字符集 支持中文的字符集是utf8,该设置可以更改mySQL配置文件进行全局设置,也可以针对数据库设置,也可以针对表设置,也可以针对列设置.字符集更改后新插入的数据生效,对以前不生效. ...

  3. Linux系统管理员非常使用的几款工具推荐

    1. ExplainShell.com 命令解释 对于Linux用户来说每天都会写各种命令和脚本,那么你可以使用这个网站工具来查看命令式如何工作的,这样可以避免不必要的错误出现:也是一个很好的学习命令 ...

  4. python学习笔记--装饰器

    1.首先是一个很无聊的函数,实现了两个数的加法运算: def f(x,y): print x+y f(2,3) 输出结果也ok 5 2.可是这时候我们感觉输出结果太单一了点,想让代码的输出多一点看起来 ...

  5. Mybatis学习(2)原始dao开发和使用mapper接口代理开发

    基础知识: 1).SqlSessionFactoryBuilder: 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory.将SqlSessionFact ...

  6. cpu监控之三:mpstat命令

    mpstat是MultiProcessor Statistics的缩写,是实时系统监控工具.报告CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有CP ...

  7. 读取 Excel 之 NPOI

    HSSFWorkbook hssfworkbook; void InitializeWorkbook(string path) { //read the template via FileStream ...

  8. jquey XMLHttpRequest cannot load url.Origin null is not allowed by Access-Control-Allow-Origin

    此篇文章原文地址:http://blog.csdn.net/wangsky2/article/details/22961345 正文: 原文地址:http://stackoverflow.com/qu ...

  9. 最近比赛中遇到的几道dp题

    1.2015 icpc 长春-H-Partial Tree(据说是完全背包,但我觉得不像) 一.题意 给定$n$个点,每一个点$i$的权值为关于度数$d_i$的函数$f(d_i),$让你构建一棵树,使 ...

  10. django的用户认证组件

    DataSource:https://www.cnblogs.com/yuanchenqi/articles/9064397.html 代码总结: 用户认证组件: 功能:用session记录登录验证状 ...