The scheme programming language--CPS

传说中的CPS,颠覆思维的little journey.

First example

(letrec ([f (lambda (x) (cons ‘a x))]
         [g (lambda (x) (cons ‘b (f x)))]
         [h (lambda (x) (g (cons ‘c x)))])
  (cons ‘d (h ‘())))

We can rewrite this in Continuation-passing style

(letrec([f (lambda (x k) (k (cons ‘a x)))]
        [g (lambda (x k) (f x (lambda(x) (cons ‘b x))))]
        [h (lambda (x k) (g (cons ‘c x) k))])
  (h ‘() (lambda(x) (cons ‘d x))))

It seems that in CPS every expression are written in the form of procedure application. Actions other than funciotn call are put in C!

CPS allows a procedure to pass more than one result to its continuation

(define car&cdr
  (lambda(p k)
    (k (car p) (cdr p))))

(car&cdr ‘(a b c)
  (lambda(x y)
    (list y x))) => ((b c) a)
(car&cdr ‘(a b c) cons) => (a b c)

Another example is that two continuation is passed. This paradigm could achieve complex control structure.

(define integer-divide
  (lambda(x y success failure)
    (if (= y 0)
        (failure "divide by zero")
        (let ([q (quotient x y)])
          (success q (- x (* q y)))))))

Codes form written with call/cc could now be done with CPS

(define product
  (let ([break k])
    (lambda(ls k)
      (let f([ls ls] [k k])
        (cond
          [(null? ls) (k 1)]
          [(= (car ls) 0) (break 0)]
          [else (f (cdr ls)
                  (lambda(x)
                    (k (* (car ls) x))))])))))

the final example is an interesting CPS map

(define map/k
  (lambda(p ls k)
    (if (null? ls)
        (k ‘())
        (p (car ls)
          (lambda(x)
            (map/k p (cdr ls)
              (lambda(y)
                (k (cons x y)))))))))

(define reciprocals
  (lambda(ls)
    (map/k (lambda(x k) (if (= x 0) "zero found" (k (/ 1 x))))
      ls
      (lambda(x) x))))

By not passing continuation k to "zero found" this piece actually achieve the effect of Call/cc.

Write a program to automatically transform code to CPS form, unbelievable! Have a try some day.

时间: 2024-10-13 06:36:07

The scheme programming language--CPS的相关文章

Go语言自述(The Go Programming Language README)

声明:本文为笔者为练习英语所做的翻译练习,原文所属者与笔者没有任何关系,翻译结果不代表原文所属者的观点.笔者不保证翻译的正确性,任何人以任何形式的对本文的引用,都是不负责任和荒谬的行为,造成的后果笔者不予负责. 原文链接所属:golang/go Go is an open source programming language that makes it easy to build simple,reliable, and efficient software. Go是一门开源的编程语言,用它可

转:从《The C Programming Language》中学到的那些编程风格和设计思想

这儿有一篇写的很好的读后感:http://www.cnblogs.com/xkfz007/articles/2566424.html 读书不是目的,关键在于思考. 很早就在水木上看到有人推荐<The C Programming Language>这本书,一直都没看,开学一个月就专心拜读了一下,并认真做了课后习题.读来收获不少,主要有两点:一是加深了自己对一些基础知识的理 解和感悟:二是从中学到了一些不错的编程风格和设计思想,这些东西虽看起来不起眼但细细嚼来还是很值得学习的.下面就从四个方面做一

The Swift Programming Language (基础部分)

简介 Swift 是一门开发 iOS, OS X 和 watchOS 应用的新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的. Swift 包含了 C 和 Objective-C 上所有基础数据类型,Int表示整型值:Double和Float表示浮点型值:Bool是布尔型值:String是文本型数据.Swift 还提供了三个基本的集合类型,Array,Set和Dictionary,详见集合类型. 就像 C 语言一样,Swift

《The Swift Programming Language》的笔记-第24页

The Swift Programming Language读书笔记学习笔记 第24页 本页主要内容有两个:打印输出和如何在swift注释代码 1 如何打印变量和常量的值? 使用println函数,细节:println函数除打印变量常量值外还输出一个换行 1) 打印常量 eg1 let hw = "hello chinagame.me" println(hw) => 可将字符串常量输出到Console里 2) 打印变量, 双引号里用 \(变量名) 输出,这个像C语言里的%s eg

[iOS翻译]《The Swift Programming Language》系列:Welcome to Swift-01

本文转载至:http://www.cnblogs.com/yangfaxian/p/3765081.html 全书目录: 一.Welcome to Swift 二.Language Guide 三.Language Reference /* 译者的废话: 几个小时前熬夜看了WWDC,各种激动,今年很有料啊!当看到Swift出来的时候,瞬间傻眼,又要学习新语言了.这篇文章来自苹果官方的<The Swift Programming Language>一书,500页左右,在苹果官网有下载.Swift

Questions that are independent of programming language. These questions are typically more abstract than other categories.

Questions that are independent of programming language.  These questions are typically more abstract than other categories. Free Language Agnostic Programming Books 97 Things Every Programmer Should Know Algorithms and Data-Structures (PDF) Algorithm

The Swift Programming Language 中文版

iSwifting社区 Swift 兴趣交流群:303868520 iOS 微信公众账号:iOSDevTip Swift 微信公众账号:SwiftDev iSwifting社区 如果你觉得这个项目不错,请点击Star一下,您的支持我们最大的动力. The Swift Programming Language 中文版 这一次,让中国和世界同步 The Swift Programming Language 中文版,布布扣,bubuko.com

The Swift Programming Language IOS8 快捷编程语言

This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programming interfaces described herein for use on Apple-branded products. This info

The C Programming Language(K&R) 扣细节随记(施工中...

各种糟糕,入坑这么久才开始看K&R的The C Programming Language学C,而且还是为了应付开学某场滚回本体的考试(虽然觉着即使复习了还会被各种吊打),废话不多说,开始施工.? ? |--> 导言 整数除法会执行舍位,故要先乘后除. 数据类型: 关键字 位长(字节) 范围 格式化字符串 char 1 bytes -128..127(或0..255,与体系结构相关) ?%c unsigned char 1bytes 0..255 ?%c,?%hhu signed char 1

The C Programming Language

其实,我已经不记得自己是第几次重读这本<The C Programming Language>了,但每次读都不禁感叹,技术书竟然也可以写得如此精炼,完美,俨然是一副美丽的画卷.能从此书进入浪漫而严谨的黑客世界不能不说是一种荣幸.因此这本书一直我向初学者们推荐的首选书籍. 但凡写过技术文章的人都知道,写这类文章最困难的地方就在于如何在细节严谨性与教学引导性之间找到合适的平衡点.如果没有教学引导性,你就很容易写出一部字典来,对于初学者来说,如果缺乏轻重缓急和一个循序渐进的过程,很可能就会不得其门而