# Mu - factorial.mu

     1 # example program: compute the factorial of 5
     2
     3 def main [
     4   local-scope
     5   x:num <- factorial 5
     6   $print [result: ], x, [
     7 ]
     8 ]
     9
    10 def factorial n:num -> result:num [
    11   local-scope
    12   load-inputs
    13   {
    14     # if n=0 return 1
    15     zero?:bool <- equal n, 0
    16     break-unless zero?
    17     return 1
    18   }
    19   # return n * factorial(n-1)
    20   x:num <- subtract n, 1
    21   subresult:num <- factorial x
    22   result <- multiply subresult, n
    23 ]
    24
    25 # unit test
    26 scenario factorial-test [
    27   run [
    28     1:num <- factorial 5
    29   ]
    30   memory-should-contain [
    31     1 <- 120
    32   ]
    33 ]