about summary refs log blame commit diff stats
path: root/archive/1.vm/tangle.mu
blob: 91f12deade5635bfc0bab86308fda0fbea63bdec (plain) (tree)
using 'def'' href='/akkartik/mu/commit/tangle.mu?h=hlt&id=1ead356219bb2eb59487d1012f837bd07ec336f5'>1ead3562 ^
1
2
3
4
5
6
7
8
9
                                                      



                                                                           
                                                                   

                
                                   
             
             
                   

 
                         
                   
                    

 
                         
                               






                                   

 
          
                      
                                                                   
                              

 
# example program: constructing functions out of order
#
# We construct a factorial function with separate base and recursive cases.
# Compare factorial.mu.
#
# This isn't a very tasteful example, just a basic demonstration of
# possibilities.

def factorial n:num -> result:num [
  local-scope
  load-inputs
  <factorial-cases>
]

after <factorial-cases> [
  # if n=0 return 1
  return-unless n, 1
]

after <factorial-cases> [
  # return n * factorial(n - 1)
  {
    break-unless n
    x:num <- subtract n, 1
    subresult:num <- factorial x
    result <- multiply subresult, n
    return result
  }
]

def main [
  1:num <- factorial 5
  # trailing space in next line is to help with syntax highlighting
  $print [result: ], 1:num, [ 
]
]