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. 

转载于:https://www.cnblogs.com/cainfeng/p/4530022.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Scheme is a general-purpose programming language, descended from Algol and Lisp, widely used in computing education and research and a broad range of industrial applications. This thoroughly updated edition of The Scheme Programming Language provides an introduction to Scheme and a definitive reference for standard Scheme, presented in a clear and concise manner. Written for professionals and students with some prior programming experience, it begins by leading the programmer gently through the basics of Scheme and continues with an introduction to some of the more advanced features of the language. The fourth edition has been substantially revised and expanded to bring the content up to date with the current Scheme standard, the Revised6 Report on Scheme. All parts of the book were updated and three new chapters were added, covering the language's new library, exception handling, and record-definition features. The book offers three chapters of introductory material with numerous examples, eight chapters of reference material, and one chapter of extended examples and additional exercises. All of the examples can be entered directly from the keyboard into an interactive Scheme session. Answers to many of the exercises, a complete formal syntax of Scheme, and a summary of forms and procedures are provided in appendixes. The Scheme Programming Language is the only book available that serves both as an introductory text in a variety of courses and as an essential reference for Scheme programmers.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值