Kurt N�rmark ©
Department of Computer Science, Aalborg University, Denmark
| Abstract Previous lecture Next lecture Index References Contents |
In this lecture we will discuss the continuation concept, as it is available in Scheme. Please notice that this lecture is only a half lecture, compared to the size of the earlier lectures in this collection |
| Introduction and motivation |
| Introduction and motivation Slide Note Contents Index References |
|
|
|
| 'Jumping' with catch and throw |
| The catch and throw idea Slide Note Contents Index References |
|
| Syntax: |
|
| Syntax: |
|
|
| A catch and throw example Slide Note Contents Index References |
|
| Program: An example using catch and throw. Please notice that the example is not a proper Scheme program. Catch and throw are not defined in Scheme. |
(define (list-length lst)
(catch 'exit
(letrec ((list-length1
(lambda (lst)
(cond ((null? lst) 0)
((pair? lst) (+ 1 (list-length1 (cdr lst))))
(else (throw 'exit 'improper-list))))))
(list-length1 lst))))
|
|
| The continuation idea |
| The intuition behind continuations Slide Note Contents Index References |
| The concept continuation: A continuation of the evaluation of an expresion E in a surrounding context C represents the entire future of the computation, which waits for the value of E |
| Table. An intuitive understanding of continuations of an expression in some context. |
|
| Being more precise Slide Note Contents Index References |
|
| Table. A more precise notation of the continuations of E |
|
| The capturing of continuations Slide Note Contents Index References |
|
| Table. Use of call/cc and capturing of continuations. |
|
| Capturing, storing, and applying continuations Slide Note Contents Index References |
|
| Table. Capturing and immediate application of the capture continuations. Notice that the vaues of the expressions E are ignored in these examples |
|
| Use of continuations for escaping purposes Slide Note Contents Index References |
|
| Table. Capturing and use of a continuation for escaping purposes |
|
| Practical examples |
| Practical example: Length of an improper list Slide Note Contents Index References |
|
| Program: The function list-length, which returns the symbol 'improper-list in case it encounters an improper list. |
(define (list-length l) (call-with-current-continuation (lambda (do-exit) (letrec ((list-length1 (lambda (l) (cond ((null? l) 0) ((pair? l) (+ 1 (list-length1 (cdr l)))) (else (do-exit 'improper-list)))))) (list-length1 l))) )) |
| Practical example: Seaching a binary tree Slide Note Contents Index References |
|
| Program: A tree search function which uses a countinuation found if we find what we search for. Notice that this examples requires the function subtree-list, in order to work. The function returns #f in case we do not find node we are looking for. Notice that it makes sense in this example to have both the if expression and the #f value in sequence! |
(define (find-in-tree tree pred) (call-with-current-continuation (lambda (found) (letrec ((find-in-tree1 (lambda (tree pred) (if (pred tree) (found tree) (let ((subtrees (subtree-list tree))) (for-each (lambda (subtree) (find-in-tree1 subtree pred)) subtrees))) #f))) (find-in-tree1 tree pred))) )) |
| Practical example: Coroutines Slide Note Contents Index References |
|
| Discussed at the black board |
| Contination passing style |
| Continuation passing style Slide Note Contents Index References |
| The concept continuation passing style: Continuation passing style is a programming technique which rely on an extra parameter to each function. The extra parameter represents the contination of the computation |
|
| Program: A determinant function programmed in direct style. |
(define (determinant a b c)
(sub (square b)
(mult (mult 4 a) c)))
(define (square a)
(mult a a))
(define (mult a b)
(* a b))
(define (sub a b)
(- a b))
|
| Program: A determinant function programming in continuation passing style. In order to apply it, pass the identity function of one parameter, (lambda (x) x), as actual parameter. |
(define (determinant a b c k0)
(mult 4 a
(lambda (v1)
(mult v1 c
(lambda(v2)
(square b
(lambda(v3)
(sub v3 v2 k0))))))))
(define (square a k1)
(mult a a k1))
(define (mult a b k2)
(k2 (* a b)))
(define (sub a b k3)
(k3 (- a b)))
|
| Source files in this lecture Contents Index References |
includes/catch-throw-ex includes/list-length.scm includes/tree-search.scm includes/determinant.scm includes/determinant-cps.scm |
Chapter 6: Continuations
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: November 6, 2001, 10:37:09