1 # Example program showing that a 'paused' continuation can be 'resumed' with
 2 # ingredients.
 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 # Expected output:
 8 #   1
 9 #   3
10 #   5
11 
12 def main [
13   local-scope
14   l:&:list:num <- copy 0
15   l <- push 3, l
16   l <- push 2, l
17   l <- push 1, l
18   k:continuation, x:num, done?:bool <- call-with-continuation-mark create-yielder, l
19   a:num <- copy 1
20   {
21   ¦ break-if done?
22   ¦ $print x 10/newline
23   ¦ k, x:num, done?:bool <- call k, a  # resume; x = a + next l value
24   ¦ a <- add a, 1
25   ¦ loop
26   }
27 ]
28 
29 def create-yielder l:&:list:num -> n:num, done?:bool [
30   local-scope
31   load-ingredients
32   a:num <- copy 0
33   {
34   ¦ done? <- equal l, 0
35   ¦ break-if done?
36   ¦ n <- first l
37   ¦ l <- rest l
38   ¦ n <- add n, a
39   ¦ a <- return-continuation-until-mark n, done?  # pause/resume
40   ¦ loop
41   }
42   return-continuation-until-mark -1, done?
43   assert 0/false, [called too many times, ran out of continuations to return]
44 ]