TWiki . Doc . AckermannNoviceExample

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 

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

   \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

----- Revision r1.9 - 28 Apr 2005 - 11:47 GMT - TWikiGuest
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.