The scheme programming language--continuation

开始学习大名鼎鼎的continuation,脑袋转起来吧.

continuation is just a procedure that accepts one single variable.

call/cc catch the current continuation------current continuation means what the system are going to do with the result of (call/cc expression)

In example, (call/cc (lambda(k).....))this continuation is stored in k. whenever k is invoked. We return to that situation

(call/cc
    (lambda(k)
        (* 5 (k 4))))  => 4

(+ 2
    (call/cc
        (lambda(k)
            (* 5 (k 4))))) => 6

A more useful example

(define product
  (lambda(ls)
    (call/cc
      (lambda(break)
        (let f ([ls ls])
          (cond
            [(null? ls) 0]
            [(= (car ls) 0) (break 0)]
            [else (* (car ls) (f (cdr ls))]))))))

A very subtle example

(let ([x (call/cc (lambda(k) k))])
  (x (lambda(ignore) "hey!")))

the behavior of this piece needs careful understanding.

The value binded to x is like

(lambda(var)
  (let ([x var])
    (x (lambda(ignore) "hey!"))))

when this procedure applys to (lambda(ignore) "hey!")), result in printing "hey"

 Another subtle one

(((call/cc (lambda (k) k))  (lambda (x) x))  "HEY!") => "HEY!"

Continuation could be saved in top-level env. And can be invoke whenever necessary

(define retry #f)

(define factorial
  (lambda (x)
    (if (= x 0)
        (call/cc (lambda (k) (set! retry k) 1))
        (* x (factorial (- x 1))))))

(factorial 4) => 24
(retry 1) => 24 (retry 5) => 120

Continuation is a whole procedure during a process about what to do with a single value. In this example this procedure include all the operation on the base value till the procedure returns.

The last example is fairly interesting and useful. A very cool application to do a ligh-weight multitasking job

(define lwp-list '())
(define lwp
  (lambda(thunk)
    (set!  lwp-list (append lwp-list (list thunk)))))
(define start
  (lambda()
    (let ([p (car lwp-list)])
      (set! lwp-list (cdr lwp-list))
      (p))))
(define pause
  (lambda()
    (call/cc
      (lambda(k)
        (lwp (lambda() (k #f))
        (start)))))
(lwp (lambda () (let f () (pause) (display "b") (f)))))
(lwp (lambda () (let f () (pause) (display "i") (f)))))
(lwp (lambda () (let f () (pause) (display "t") (f)))))
(lwp (lambda () (let f () (pause) (display "c") (f)))))
(lwp (lambda () (let f () (pause) (display "h") (f)))))
(lwp (lambda () (let f () (pause) (display "!") (f)))))
(lwp (lambda () (let f () (pause) (newline) (f)))))
(start) => where amazing happens

Analyse the complex behavior step by step

(lwp (lambda () (let f () (pause) (display "b") (f))))))

At first, put these thunks on the the excution list one by one

Then ”start". pop out the first (thunk) and execute it. It is a recursive procedure. (pause) is invoked soon. (pause) append the bomb to the back end. Then continue read from the execute list.

When come to the bomb. The continuation is invoked. The continuation should 1. print "b" 2.invoke f again. That's it! As literally defined.

f->(pause)->||->print "b"->f->(pause)->||->.......

SO COOL!!

 

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

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、付费专栏及课程。

余额充值