Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language X10 1.7

(IBM language for DARPA HPCS)

Date:05/04/10
Author:James LaGrone
URL:n/a
Comments:0
Info:http://x10.codehaus.org/
Score: (2.00 in 2 votes)
/***********************************************************************
 * X10 implementation of "99 bottles of beer"
 *
 * by James LaGrone (with a few ideas from the Chapel version)
 * 
 * compiles and runs with x10 compiler version 1.7.7 with Java backend
 *
 *		x10c -commandlineonly NinetyNineBottles.x10
 *
 ***********************************************************************/

public class NinetyNineBottles{
	
	public static def main(args: Rail[String]){
		val beerSong: NinetyNineBottles = NinetyNineBottles.make(99);
		beerSong.create();
		beerSong.sing();
	}	
	var numVerses: Int;
	var verses: Array[String]{rank==1};
	var bottles: Int;
	
	public static def make(var numBottles: Int){
		val s = new NinetyNineBottles();
		s.numVerses = numBottles + 1;
		val verses: Region{rank==1} = [1..s.numVerses];
		s.verses = Array.make[String](verses, (Point) => new String());
		s.bottles = numBottles;
		return s;
	}
	
	public def create(){
		finish ateach ( (p):Point in verses ){
			val wallCount = bottles - p + 1;
			verses(p) = createVerse( p ) ;
		}		
		return;
	}
	
	public def sing(){
		for ( (p):Point in verses ){
			Console.OUT.println(verses(p));
		}
		return;
	}
		
	public def createVerse(var verseNum: Int): String{
		val lineA:String, 
			 lineB:String;
		val wallCount = bottles - (verseNum - 1);
		val nextWallCount = (wallCount + numVerses - 1)%numVerses;

		if (wallCount > 0) {
			lineA = line(wallCount);
			lineB = lineA;
		}else{
			lineA = "No " + line(wallCount);
			lineB = "no " + line(wallCount);
		}
		
		return lineA + " on the wall, " + lineB + ".\n" + 
			action(wallCount) + line(nextWallCount) + " on the wall.\n";
	}
	
	public static def line(val numBottles:Int):String{
		
		switch(numBottles){			
			case 0:	return "more bottles of beer";
			case 1:	return numBottles + " bottle of beer";
			default:	return numBottles + " bottles of beer";
	  }
  }	

	public static def action(val numBottles:Int):String{
		if (numBottles==0) 
			return "Go to the store and buy some more, ";
		else
			return "Take one down and pass it around, ";
	}
	
}

Download Source | Write Comment

Alternative Versions

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: