1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.h# Example program showing that a 'paused' continuation can be 'resumed' with
# inputs.
#
# Print out a list of numbers, first adding 0 to the first, 1 to the second, 2
# to the third, and so on.
#
# To run:
# $ git clone https://github.com/akkartik/mu
# $ cd mu
# $ ./mu continuation5.mu
#
# Expected output:
# 1
# 3
# 5
def main [
local-scope
l:&:list:num <- copy 0
l <- push 3, l
l <- push 2, l
l <- push 1, l
k:continuation, x:num, done?:bool <- call-with-continuation-mark 100/mark, create-yielder, l
a:num <- copy 1
{
break-if done?
$print x 10/newline
k, x:num, done?:bool <- call k, a # resume; x = a + next l value
a <- add a, 1
loop
}
]
def create-yielder l:&:list:num -> n:num, done?:bool [
local-scope
load-inputs
a:num <- copy 0
{
done? <- equal l, 0
break-if done?
n <- first l
l <- rest l
n <- add n, a
a <- return-continuation-until-mark 100/mark, n, done? # pause/resume
loop
}
return-continuation-until-mark 100/mark, -1, done?
assert 0/false, [called too many times, ran out of continuations to return]
]
|