The scheme programming language--continuation
2015-05-25 10:28
232 查看
开始学习大名鼎鼎的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
A more useful example
A very subtle example
the behavior of this piece needs careful understanding.
The value binded to x is like
when this procedure applys to (lambda(ignore) "hey!")), result in printing "hey"
Another subtle one
Continuation could be saved in top-level env. And can be invoke whenever necessary
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
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!!
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!!
相关文章推荐
- The scheme programming language--General description
- The scheme programming language--CPS
- 需要中文版《The Scheme Programming Language》的朋友可以在此留言(内附一小段译文)
- <<The C Programming Language>>学习之路-练习题参考答案 1-12
- The C# Programming Language Notes
- C语言中各种数据类型长度的总结:(参考The C Programming Language)
- 《The C++ programming language》读书笔记(1)——第一章:致读者
- 转:Password Masking in the Java Programming Language
- The Swift Programming Language-官方教程精译Swift(6)控制流--Control Flow
- The Swift Programming Language
- 《The C++ Programming Language》摘抄与总结——类型与声明
- 编写函数itob,将整数n转换为以b为底的数(The c programming language 练习3-5)
- The C# Programming Language Notes II
- Ada 程序设计语言(The Ada Programming Language)[第四集]
- Functions(Chapter 7 of The C++ Programming Language)
- Namespaces and Exceptions(Chapter 8 of The C++ Programming Language)
- Scheme Programming language II
- The C Programming Language P8.5
- The Swift Programming Language中文/英文版
- The C Programming Language 练习题3-5