Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

36
LINES

< > BotCompany Repo | #1030381 // Turn Ackermann function into a hibernatable computation

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (5972L/31K).

cprint {
  /* // standard version
     static int ackermann(int a, int b) {
       if (a == 0) ret b+1;
       if (b == 0) ret ackermann(a-1, 1);
       ret ackermann(a-1, ackermann(a, b-1));
     }
  */

  // hibernatable version
  srecord ackermann(int a, int b) implements VStack.Computable<Int> {
    void step(VStack stack, O subComputationResult) {
      if (subComputationResult == null) {
        if (a == 0)
          stack._return(b+1);
        else if (b == 0)
          stack.tailCall(new ackermann(a-1, 1));
        else
          stack.push(new ackermann(a, b-1));
      } else
        stack.tailCall(new ackermann(a-1, subComputationResult/Int));
    }
  }
  
  start-thread {
    print("Result: " + vStackComputeWithStats(new ackermann(2, 2)));
    
    // detailed run
    VStack stack = new(new ackermann(2, 2));
    printStruct(stack);
    while ping (stack.step()) {
      printStruct(stack);
      stack = restructure(stack); // persist to string and re-create
    }
  }
}

Author comment

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: 147 / 289
Version history: 12 change(s)
Referenced in: [show references]