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. 高大上的JS工具

    EtherPad (协同文件编辑): EtherCalc (协同电子表格编辑)

  2. C#继承基本控件实现自定义控件 (转帖)

    自定义控件分三类: 1.复合控件:基本控件组合而成.继承自UserControl 2.扩展控件:继承基本控件,扩展一些属性与事件.比如继承Button 3.自定义控件:直接继承自Control 第一种 ...

  3. java成员内部类

    java成员内部类依赖于外部类而存在,故创建内部类需要首先创建其关联的外部类. public class Test { public static void main(String args[]) { ...

  4. phper必知必会之类库自动加载的七种方式(三)

    ## php自动加载 下面显示例子的文件目录结构图 一.没有使用命名空间的几种实现 test/oneClass.php class oneClass{ public function show(){ ...

  5. [OI向?] ubuntu下一些常用的技巧

    想起来什么就写什么吧. Ubuntu下的对拍程序 python是最为简便的. from os import system while True: system("./make > in ...

  6. 介绍了如何取成员函数的地址以及调用该地址:C++

    摘要:介绍了如何取成员函数的地址以及调用该地址. 关键字:C++成员函数 this指针 调用约定 一.成员函数指针的用法 在C++中,成员函数的指针是个比较特殊的东西.对普通的函数指针来说,可以视为一 ...

  7. [UE4]C++中引用(&)的用法和应用实例

    对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率 ...

  8. 如何捕捉@tornado.gen.coroutine里的异常

    from tornado import gen from tornado.ioloop import IOLoop @gen.coroutine def throw(a,b): try: a/b ra ...

  9. iOS whose view is not in the window hierarchy!

    解决方法: viewController只Load完毕,没有Appear,此时应该将语句转移到ViewDidAppear方法中

  10. Hadoop 2.7.3 分布式集群安装

    1. 集群规划: 192.168.1.252 palo252 Namenode+Datanode 192.168.1.253 palo253 YarnManager+Datanode+Secondar ...