1 # Example program showing 'return-continuation-until-mark' return other values
 2 # alongside continuations.
 3 #
 4 # Print out a given list of numbers.
 5 #
 6 # To run:
 7 #   $ git clone https://github.com/akkartik/mu1
 8 #   $ cd mu1
 9 #   $ git checkout 4a48bedcd1  # state as of this writing; later versions may be incompatible
10 #   $ ./mu continuation4.mu
11 #
12 # Expected output:
13 #   1
14 #   2
15 #   3
16 
17 def main [
18   local-scope
19   l:&:list:num <- copy 0
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 create-yielder, l
24   {
25   ¦ break-if done?
26   ¦ $print x 10/newline
27   ¦ k, x:num, done?:bool <- call k
28   ¦ loop
29   }
30 ]
31 
32 def create-yielder l:&:list:num -> n:num, done?:bool [
33   local-scope
34   load-inputs
35   {
36   ¦ done? <- equal l, 0
37   ¦ break-if done?
38   ¦ n <- first l
39   ¦ l <- rest l
40   ¦ return-continuation-until-mark n, done?
41   ¦ loop
42   }
43   # A function that returns continuations shouldn't get the opportunity to
44   # return. Calling functions should stop calling its continuation after this
45   # point.
46   return-continuation-until-mark -1, done?
47   assert 0/false, [called too many times, ran out of continuations to return]
48 ]