Last active
December 26, 2016 21:14
-
-
Save ePaul/b8c64b595db58fcbd26fd4884168f25f to your computer and use it in GitHub Desktop.
Ceylon Web Runner: Ackermann iterative implementation in Ceylon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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)``"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module web_ide_script "1.0.0" { | |
// Add module imports here | |
import ceylon.collection "1.3.1"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Click here to run this code online