summaryrefslogtreecommitdiff
path: root/appl/math/fibonacci.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/math/fibonacci.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/math/fibonacci.b')
-rw-r--r--appl/math/fibonacci.b59
1 files changed, 59 insertions, 0 deletions
diff --git a/appl/math/fibonacci.b b/appl/math/fibonacci.b
new file mode 100644
index 00000000..af4e769c
--- /dev/null
+++ b/appl/math/fibonacci.b
@@ -0,0 +1,59 @@
+implement Fibonacci;
+
+include "sys.m";
+include "draw.m";
+
+Fibonacci: module
+{
+ init: fn(nil: ref Draw->Context, argv: list of string);
+};
+
+init(nil: ref Draw->Context, nil: list of string)
+{
+ sys := load Sys Sys->PATH;
+ for(i := 0; ; i++){
+ f := fibonacci(i);
+ if(f < 0)
+ break;
+ sys->print("F(%d) = %d\n", i, f);
+ }
+}
+
+FIB: exception(int, int);
+HELP: con "help";
+
+NOVAL: con -1000000000;
+
+fibonacci(n: int): int
+{
+ {
+ fib(1, n, 1, 1);
+ }
+ exception e{
+ FIB =>
+ (x, nil) := e;
+ return x;
+ * =>
+ return NOVAL;
+ }
+ return NOVAL;
+}
+
+fib(n: int, m: int, x: int, y: int) raises (FIB)
+{
+ if(n >= m)
+ raise FIB(x, y);
+
+ {
+ fib(n+1, m, x, y);
+ }
+ exception e{
+ FIB =>
+ (x, y) = e;
+ x = x+y;
+ y = x-y;
+ raise FIB(x, y);
+ * =>
+ raise HELP;
+ }
+}