Skip to content

Commit 31c9086

Browse files
committed
(docs) improve continuation page
1 parent e70d958 commit 31c9086

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

docs/docs/scheme-intro/continuations.md

+46-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and `<slot>` is an expression: (e.g.: `(/ 1 10)`)
2121

2222
```scheme
2323
(+ 1 2 (/ 1 10))
24+
;; ==> 31/10
2425
```
2526

2627
then continuation for expression `(/ 1 10)` is `(+ 1 2 <slot>)`. Continuations is the next
@@ -29,17 +30,36 @@ expression that needs to be evaluated.
2930
Scheme is unique because it allows accessing continuations. They are first class objects like
3031
numbers or functions.
3132

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+
3243
## Accessing current continuation
3344

3445
To access the current continuation for an expression, you need to use `call-with-current-continuation`
3546
or its abbreviation `call/cc`. The procedure `call/cc` accepts a single procedure that get the
3647
continuation as first argument:
3748

3849
```scheme
39-
(call/cc (lambda (c)
50+
(call/cc (lambda (cc)
4051
...))
4152
```
4253

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+
4363
The continuation saved in `c` capture whole state of the Scheme interpreter. The continuation act as
4464
a procedure that you can pass a single value to it and Scheme will jump in to the place where
4565
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.
5171
```scheme
5272
(define k #f)
5373
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
6181
```
6282

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)`).
6585

6686
:::info
6787

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.
70104

71105
:::
72106

0 commit comments

Comments
 (0)