1 # Example program showing that a 'paused' continuation can be 'resumed' with
 2 # inputs.
 3 #
 4 # Print out a list of numbers, first adding 0 to the first, 1 to the second, 2
 5 # to the third, and so on.
 6 #
 7 # To run:
 8 #   $ git clone https://github.com/akkartik/mu
 9 #   $ cd mu
10 #   $ ./mu continuation5.mu
11 #
12 # Expected output:
13 #   1
14 #   3
15 #   5
16 
17 def main [
18   local-scope
19   l:&:list:num <- copy null
20   l <- push 3, l
21   l <- push 2, l
22   l <- push 1, l
23   k:continuation, x:num, done?:bool <- call-with-continuation-mark 100/mark, create-yielder, l
24   a:num <- copy 1
25   {
26     break-if done?
27     $print x 10/newline
28     k, x:num, done?:bool <- call k, a  # resume; x = a + next l value
29     a <- add a, 1
30     loop
31   }
32 ]
33 
34 def create-yielder l:&:list:num -> n:num, done?:bool [
35   local-scope
36   load-inputs
37   a:num <- copy 0
38   {
39     done? <- equal l, null
40     break-if done?
41     n <- first l
42     l <- rest l
43     n <- add n, a
44     a <- return-continuation-until-mark 100/mark, n, done?  # pause/resume
45     loop
46   }
47   return-continuation-until-mark 100/mark, -1, done?
48   assert false, [called too many times, ran out of continuations to return]
49 ]