2016-03-09 22:54:41 +00:00
|
|
|
# example program: constructing functions out of order
|
2015-05-11 19:00:50 +00:00
|
|
|
#
|
|
|
|
# We construct a factorial function with separate base and recursive cases.
|
|
|
|
# Compare factorial.mu.
|
|
|
|
#
|
2017-09-24 01:31:26 +00:00
|
|
|
# This isn't a very tasteful example, just a basic demonstration of
|
2015-05-06 06:50:50 +00:00
|
|
|
# possibilities.
|
|
|
|
|
2016-09-17 07:43:13 +00:00
|
|
|
def factorial n:num -> result:num [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2017-09-24 01:31:26 +00:00
|
|
|
<factorial-cases>
|
2015-05-06 06:50:50 +00:00
|
|
|
]
|
|
|
|
|
2017-09-24 01:31:26 +00:00
|
|
|
after <factorial-cases> [
|
2015-05-06 06:50:50 +00:00
|
|
|
# if n=0 return 1
|
2017-09-24 01:31:26 +00:00
|
|
|
return-unless n, 1
|
2015-05-06 06:50:50 +00:00
|
|
|
]
|
|
|
|
|
2017-09-24 01:31:26 +00:00
|
|
|
after <factorial-cases> [
|
2015-05-06 06:50:50 +00:00
|
|
|
# return n * factorial(n - 1)
|
2017-09-24 01:31:26 +00:00
|
|
|
{
|
|
|
|
break-unless n
|
|
|
|
x:num <- subtract n, 1
|
|
|
|
subresult:num <- factorial x
|
|
|
|
result <- multiply subresult, n
|
|
|
|
return result
|
|
|
|
}
|
2015-05-06 06:50:50 +00:00
|
|
|
]
|
|
|
|
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- factorial 5
|
2016-09-07 07:21:39 +00:00
|
|
|
# trailing space in next line is to help with syntax highlighting
|
2016-09-17 07:43:13 +00:00
|
|
|
$print [result: ], 1:num, [
|
2015-05-06 06:50:50 +00:00
|
|
|
]
|
|
|
|
]
|