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 #   $ git checkout 4a48bedcd1  # state as of this writing; later versions may be incompatible
11 #   $ ./mu continuation5.mu
12 #
13 # Expected output:
14 #   1
15 #   3
16 #   5
17 
18 def main [
19   local-scope
20   l:&:list:num <- copy 0
21   l <- push 3, l
22   l <- push 2, l
23   l <- push 1, l
24   k:continuation, x:num, done?:bool <- call-with-continuation-mark create-yielder, l
25   a:num <- copy 1
26   {
27   ¦ break-if done?
28   ¦ $print x 10/newline
29   ¦ k, x:num, done?:bool <- call k, a  # resume; x = a + next l value
30   ¦ a <- add a, 1
31   ¦ loop
32   }
33 ]
34 
35 def create-yielder l:&:list:num -> n:num, done?:bool [
36   local-scope
37   load-inputs
38   a:num <- copy 0
39   {
40   ¦ done? <- equal l, 0
41   ¦ break-if done?
42   ¦ n <- first l
43   ¦ l <- rest l
44   ¦ n <- add n, a
45   ¦ a <- return-continuation-until-mark n, done?  # pause/resume
46   ¦ loop
47   }
48   return-continuation-until-mark -1, done?
49   assert 0/false, [called too many times, ran out of continuations to return]
50 ]