@@ -21,6 +21,7 @@ and `<slot>` is an expression: (e.g.: `(/ 1 10)`)
21
21
22
22
``` scheme
23
23
(+ 1 2 (/ 1 10))
24
+ ;; ==> 31/10
24
25
```
25
26
26
27
then continuation for expression ` (/ 1 10) ` is ` (+ 1 2 <slot>) ` . Continuations is the next
@@ -29,17 +30,36 @@ expression that needs to be evaluated.
29
30
Scheme is unique because it allows accessing continuations. They are first class objects like
30
31
numbers or functions.
31
32
33
+ You can be represented continuation as a function:
34
+
35
+ if ` (/ 1 10) ` is ` z ` the contunuation of the whole expression can be represented as a function,
36
+ that may look like this:
37
+
38
+ ``` scheme
39
+ (lambda (z)
40
+ (+ 1 2 z))
41
+ ```
42
+
32
43
## Accessing current continuation
33
44
34
45
To access the current continuation for an expression, you need to use ` call-with-current-continuation `
35
46
or its abbreviation ` call/cc ` . The procedure ` call/cc ` accepts a single procedure that get the
36
47
continuation as first argument:
37
48
38
49
``` scheme
39
- (call/cc (lambda (c )
50
+ (call/cc (lambda (cc )
40
51
...))
41
52
```
42
53
54
+ So to capute the continuation expressed as a function you can use code like this:
55
+
56
+ ``` scheme
57
+ (+ 1 2 (call/cc
58
+ (lambda (c)
59
+ (/ 1 10))))
60
+ ;; ==> 31/10
61
+ ```
62
+
43
63
The continuation saved in ` c ` capture whole state of the Scheme interpreter. The continuation act as
44
64
a procedure that you can pass a single value to it and Scheme will jump in to the place where
45
65
continuation was captured with a given value.
@@ -51,22 +71,36 @@ You can save continuation inside a variable and call it later like a procedure.
51
71
``` scheme
52
72
(define k #f)
53
73
54
- (+ 1 (call/cc
55
- (lambda (continuation)
56
- (set! k continuation)
57
- 2 )))
58
- ;; ==> 3
59
- (k 10 )
60
- ;; ==> 11
74
+ (display ( + 1 2 (call/cc
75
+ (lambda (continuation)
76
+ (set! k continuation)
77
+ (/ 1 10)) )))
78
+ ;; ==> 31/10
79
+ (k 3 )
80
+ ;; ==> 6
61
81
```
62
82
63
- Here when you call a continuation ` k ` with value 10 it restores the state in ` (+ 1 <slot>) ` and
64
- execute that expression again with a value ` 10 ` (expression ` (+ 1 10 ) ` ).
83
+ Here when you call a continuation ` k ` with value 6 it restores the state in ` (+ 1 2 <slot>) ` and
84
+ execute that expression again with a value ` 3 ` (expression ` (+ 1 2 3 ) ` ).
65
85
66
86
::: info
67
87
68
- Note that the above code will create an infinite loop when called inside an expression like ` let ` .
69
- It will only work at top level.
88
+ Note that the above code will create an infinite loop when called inside an expression like ` let ` :
89
+
90
+ ``` scheme
91
+ (let ()
92
+ (define k #f)
93
+ (display (+ 1 2 (call/cc
94
+ (lambda (continuation)
95
+ (set! k continuation)
96
+ (/ 1 10)))))
97
+
98
+ (newline)
99
+ (k 3)) ;; start infinite loop
100
+ ```
101
+
102
+ Above code will print 6 in an ifnite loop, because the contination don't end at display, like in
103
+ previous exaple, only continue executing newline and next call to the continuation.
70
104
71
105
:::
72
106
0 commit comments