Skip to content

Instantly share code, notes, and snippets.

@ePaul
Last active December 26, 2016 21:14
Show Gist options
  • Save ePaul/b8c64b595db58fcbd26fd4884168f25f to your computer and use it in GitHub Desktop.
Save ePaul/b8c64b595db58fcbd26fd4884168f25f to your computer and use it in GitHub Desktop.
Ceylon Web Runner: Ackermann iterative implementation in Ceylon
import ceylon.collection {
Stack,
ArrayList
}
Integer ackermann2(Integer n0, Integer m0) {
Stack<Integer> stack = ArrayList<Integer>();
stack.push(n0);
stack.push(m0);
while (true) {
print(stack);
value m = stack.pop();
assert (exists m);
if (exists n = stack.pop()) {
if (n == 0) {
stack.push(m + 1);
} else if (m == 0) {
stack.push(n - 1);
stack.push(1);
} else {
stack.push(n - 1);
stack.push(n);
stack.push(m - 1);
}
} else {
// stack is empty
return m;
}
// uncomment to step through the program
//process.readLine();
}
}
import ceylon.collection { Stack, ArrayList }
Integer ackermannOld(Integer n0, Integer m0) {
Stack<Integer|[Integer,Integer]> stack = ArrayList<Integer|[Integer,Integer]>();
stack.push([n0, m0]);
while(true) {
print(stack);
switch(result = stack.pop())
case (is Integer) {
if(exists prev = stack.pop()) {
// there is more to do, put both items together on the stack
assert(is Integer prev);
stack.push([prev, result]);
} else {
// stack is empty
return result;
}
}
case ([Integer n, Integer m]) {
if(n == 0) {
stack.push(m+1);
} else if (m == 0) {
stack.push([n-1, 1]);
} else {
stack.push(n-1);
stack.push([n, m-1]);
}
}
case (null) {
// should not happen
assert(false);
}
// process.readLine();
}
}
/**
* Main entry point. Simply edit n and m here to test with different parameters.
*/
shared void run() {
value n = 3;
value m = 3;
print("a(``n``,``m``) = ``ackermann2(n,m)``");
}
module web_ide_script "1.0.0" {
// Add module imports here
import ceylon.collection "1.3.1";
}
@ePaul
Copy link
Author

ePaul commented Dec 26, 2016

Click here to run this code online

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment