码迷,mamicode.com
首页 > 其他好文 > 详细

The scheme programming language--CPS

时间:2015-05-26 20:52:46      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

传说中的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. 

The scheme programming language--CPS

标签:

原文地址:http://www.cnblogs.com/cainfeng/p/4530022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!