Uses 911K of libraries. Click here for Pure Java version (5972L/31K).
1 | cprint { |
2 | /* // standard version |
3 | static int ackermann(int a, int b) { |
4 | if (a == 0) ret b+1; |
5 | if (b == 0) ret ackermann(a-1, 1); |
6 | ret ackermann(a-1, ackermann(a, b-1)); |
7 | } |
8 | */ |
9 | |
10 | // hibernatable version |
11 | srecord ackermann(int a, int b) implements VStack.Computable<Int> { |
12 | void step(VStack stack, O subComputationResult) { |
13 | if (subComputationResult == null) { |
14 | if (a == 0) |
15 | stack._return(b+1); |
16 | else if (b == 0) |
17 | stack.tailCall(new ackermann(a-1, 1)); |
18 | else |
19 | stack.push(new ackermann(a, b-1)); |
20 | } else |
21 | stack.tailCall(new ackermann(a-1, subComputationResult/Int)); |
22 | } |
23 | } |
24 | |
25 | start-thread { |
26 | print("Result: " + vStackComputeWithStats(new ackermann(2, 2))); |
27 | |
28 | // detailed run |
29 | VStack stack = new(new ackermann(2, 2)); |
30 | printStruct(stack); |
31 | while ping (stack.step()) { |
32 | printStruct(stack); |
33 | stack = restructure(stack); // persist to string and re-create |
34 | } |
35 | } |
36 | } |
Began life as a copy of #1030379
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030381 |
Snippet name: | Turn Ackermann function into a hibernatable computation |
Eternal ID of this version: | #1030381/13 |
Text MD5: | 922f8b9de254445b64b006e051fc0ea8 |
Transpilation MD5: | 92faebee621066aaee913dc8b4b8168a |
Author: | stefan |
Category: | javax / maths |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-12-10 15:56:07 |
Source code size: | 993 bytes / 36 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 220 / 384 |
Version history: | 12 change(s) |
Referenced in: | [show references] |