examples
This commit is contained in:
43
examples/demo.monkey
Normal file
43
examples/demo.monkey
Normal file
@@ -0,0 +1,43 @@
|
||||
let name = "Monkey";
|
||||
let age = 1;
|
||||
let inspirations = ["Scheme", "Lisp", "JavaScript", "Clojure"];
|
||||
let book = {
|
||||
"title": "Writing A Compiler In Go",
|
||||
"author": "Thorsten Ball",
|
||||
"prequel": "Writing An Interpreter In Go"
|
||||
};
|
||||
|
||||
let printBookName = fn(book) {
|
||||
let title = book["title"];
|
||||
let author = book["author"];
|
||||
puts(author + " - " + title);
|
||||
};
|
||||
|
||||
printBookName(book);
|
||||
|
||||
let fibonacci = fn(x) {
|
||||
if (x == 0) {
|
||||
0
|
||||
} else {
|
||||
if (x == 1) {
|
||||
return 1;
|
||||
} else {
|
||||
fibonacci(x - 1) + fibonacci(x - 2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let map = fn(arr, f) {
|
||||
let iter = fn(arr, accumulated) {
|
||||
if (len(arr) == 0) {
|
||||
accumulated
|
||||
} else {
|
||||
iter(rest(arr), push(accumulated, f(first(arr))));
|
||||
}
|
||||
};
|
||||
|
||||
iter(arr, []);
|
||||
};
|
||||
|
||||
let numbers = [1, 1 + 1, 4 - 1, 2 * 2, 2 + 3, 12 / 2];
|
||||
map(numbers, fibonacci);
|
||||
11
examples/fib.monkey
Normal file
11
examples/fib.monkey
Normal file
@@ -0,0 +1,11 @@
|
||||
let fib = fn(x) {
|
||||
if (x == 0) {
|
||||
return 0
|
||||
}
|
||||
if (x == 1) {
|
||||
return 1
|
||||
}
|
||||
return fib(x-1) + fib(x-2)
|
||||
}
|
||||
|
||||
puts(fib(35))
|
||||
11
examples/fibt.monkey
Normal file
11
examples/fibt.monkey
Normal file
@@ -0,0 +1,11 @@
|
||||
let fib = fn(n, a, b) {
|
||||
if (n == 0) {
|
||||
return a
|
||||
}
|
||||
if (n == 1) {
|
||||
return b
|
||||
}
|
||||
return fib(n - 1, b, a + b)
|
||||
}
|
||||
|
||||
puts(fib(35, 0, 1))
|
||||
Reference in New Issue
Block a user