Nice TWiki > Doc > CodeExamples > AckermannNoviceExample (r1.3) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/*
From the command line
1) create a directory ackermann
2) cd into directory ackermann
3) save this source code as main.nice in directory ackermann
4) compile the source code
   \ackermann> nicec --sourcepath=.. -a ackermann.jar ackermann
5) run the jar file
   \ackermann> java -jar ackermann.jar 8
*/


void main(String[] args){
   let n = toSingleInt(args);   
   println("Ack(3," + n + "): " + ack(3,n)); 
}

int ack(int m, int n){
   if (m == 0) return n + 1;
   if (n == 0) return ack(m-1, 1);
   return ack(m-1, ack(m, n-1)); 
}

int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }



/* Notes - Compile & Run
The Nice compiler compiles PACKAGES (directories) not files, 
so we must tell the compiler which directory contains the 
package directory (ackermann) not which directory contains
the file (main.nice).

   \projects> nicec ackermann 
   OK

   \projects\ackermann> nicec --sourcepath=.. ackermann 
   OK

   \projects\ackermann> nicec ackermann 
   FAILS! the compiler will try to find \projects\ackermann\ackermann


Let's make the compiler output to a jar file (ackermann.jar):

   \projects\ackermann> nicec --sourcepath=.. -a ackermann.jar ackermann
   nice.lang: parsing
   ackermann: parsing
   ackermann: typechecking
   ackermann: generating code
   ackermann: linking
   ackermann: writing in archive
   nice.lang: writing in archive


Let's run the program:

   \projects\ackermann> java -jar ackermann.jar
   Ack(3,1): 13

   \projects\ackermann> java -jar ackermann.jar 8
   Ack(3,8): 2045
*/

-- IsaacGouy - 26 Aug 2003

Topic AckermannNoviceExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.9 | > | r1.8 | > | r1.7 | More }
Revision r1.3 - 27 Aug 2003 - 16:27 GMT - IsaacGouy
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.

Doc.AckermannNoviceExample moved from Doc.AckermannBeginnerExample on 27 Aug 2003 - 16:27 by IsaacGouy - put it back