# Mu - counters.mu

     1 # example program: maintain multiple counters with isolated lexical scopes
     2 # (spaces)
     3
     4 def new-counter n:num -> default-space:space [
     5   default-space <- new location:type, 30
     6   load-inputs  # initialize n
     7 ]
     8
     9 def increment-counter outer:space/names:new-counter, x:num -> n:num/space:1 [
    10   local-scope
    11   load-inputs
    12   0:space/names:new-counter <- copy outer  # setup outer space; it *must* come from 'new-counter'
    13   n/space:1 <- add n/space:1, x
    14 ]
    15
    16 def main [
    17   local-scope
    18   # counter A
    19   a:space/names:new-counter <- new-counter 34
    20   # counter B
    21   b:space/names:new-counter <- new-counter 23
    22   # increment both by 2 but in different ways
    23   increment-counter a, 1
    24   b-value:num <- increment-counter b, 2
    25   a-value:num <- increment-counter a, 1
    26   # check results
    27   $print [Contents of counters], 10/newline
    28   $print [a: ], a-value, [ b: ], b-value, 10/newline
    29 ]