2014-11-27 03:11:14 +00:00
|
|
|
; To demonstrate generic functions, we'll construct a factorial function with
|
2014-12-13 01:54:31 +00:00
|
|
|
; separate base and recursive clauses. Compare factorial.mu.
|
2014-11-27 03:11:14 +00:00
|
|
|
|
2014-12-13 02:07:30 +00:00
|
|
|
; factorial n = n*factorial(n-1)
|
|
|
|
(function factorial [
|
2015-01-03 02:13:04 +00:00
|
|
|
(default-space:space-address <- new space:literal 30:literal)
|
2014-12-14 21:21:32 +00:00
|
|
|
(n:integer <- input 0:literal)
|
|
|
|
(x:integer <- subtract n:integer 1:literal)
|
|
|
|
(subresult:integer <- factorial x:integer)
|
|
|
|
(result:integer <- multiply subresult:integer n:integer)
|
|
|
|
(reply result:integer)
|
2014-11-27 03:11:14 +00:00
|
|
|
])
|
|
|
|
|
2014-12-13 02:07:30 +00:00
|
|
|
; factorial 0 = 1
|
2015-01-15 05:51:27 +00:00
|
|
|
(function factorial [
|
|
|
|
(default-space:space-address <- new space:literal 30:literal)
|
|
|
|
(n:integer <- input 0:literal)
|
2014-11-27 03:11:14 +00:00
|
|
|
{ begin
|
2014-12-14 21:21:32 +00:00
|
|
|
(zero?:boolean <- equal n:integer 0:literal)
|
|
|
|
(break-unless zero?:boolean)
|
|
|
|
(reply 1:literal)
|
2014-11-27 03:11:14 +00:00
|
|
|
}
|
|
|
|
])
|
|
|
|
|
2014-12-13 02:07:30 +00:00
|
|
|
(function main [
|
2014-12-14 21:21:32 +00:00
|
|
|
(1:integer <- factorial 5:literal)
|
2015-02-01 08:15:43 +00:00
|
|
|
($print (("result: " literal)))
|
2015-01-18 01:00:44 +00:00
|
|
|
(print-integer nil:literal/terminal 1:integer)
|
2015-02-01 08:15:43 +00:00
|
|
|
($print (("\n" literal)))
|
2014-11-27 03:11:14 +00:00
|
|
|
])
|