Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Chapel 2.58 KB | None | 0 0
  1. /***********************************************************************
  2.  * Chapel implementation of "99 bottles of beer"
  3.  *
  4.  * by Brad Chamberlain and Steve Deitz
  5.  * 07/13/2006 in Knoxville airport while waiting for flight home from
  6.  *            HPLS workshop
  7.  * compiles and runs with chpl compiler version 0.3.3211
  8.  * for more information, contact: chapel_info@cray.com
  9.  *
  10.  *
  11.  * Notes:
  12.  * o as in all good parallel computations, boundary conditions
  13.  *   constitute the vast bulk of complexity in this code (invite Brad to
  14.  *   tell you about his zany boundary condition simplification scheme)
  15.  * o uses type inference for variables, arguments
  16.  * o relies on integer->string coercions
  17.  * o uses named argument passing (for documentation purposes only)
  18.  ***********************************************************************/
  19. // allow executable command-line specification of number of bottles
  20. // (e.g., ./a.out -snumBottles=999999)
  21. config const numBottles = 99;
  22. const numVerses = numBottles+1;
  23. // a domain to describe the space of lyrics
  24. var LyricsSpace: domain(1) = [1..numVerses];
  25. // array of lyrics
  26. var Lyrics: [LyricsSpace] string;
  27. // parallel computation of lyrics array
  28. [verse in LyricsSpace] Lyrics(verse) = computeLyric(verse);
  29. // as in any good parallel language, I/O to stdout is serialized.
  30. // (Note that I/O to a file could be parallelized using a parallel
  31. // prefix computation on the verse strings' lengths with file seeking)
  32. writeln(Lyrics);
  33. // HELPER FUNCTIONS:
  34. fun computeLyric(verseNum) {
  35.   var bottleNum = numBottles - (verseNum - 1);
  36.   var nextBottle = (bottleNum + numVerses - 1)%numVerses;
  37.   return "\n" // disguise space used to separate elements in array I/O
  38.        + describeBottles(bottleNum, startOfVerse=true) + " on the wall, "
  39.        + describeBottles(bottleNum) + ".\n"
  40.        + computeAction(bottleNum)
  41.        + describeBottles(nextBottle) + " on the wall.\n";
  42. }
  43. fun describeBottles(bottleNum, startOfVerse:bool = false) {
  44. // NOTE: bool should not be necessary here (^^^^); working around bug
  45.   var bottleDescription = if (bottleNum) then bottleNum:string
  46.                                          else (if startOfVerse then "N"
  47.                                                                else "n")
  48.                                               + "o more";
  49.   return bottleDescription
  50.        + " bottle" + (if (bottleNum == 1) then "" else "s")
  51.        + " of beer";
  52. }
  53. fun computeAction(bottleNum) {
  54.   return if (bottleNum == 0) then "Go to the store and buy some more, "
  55.                              else "Take one down and pass it around, ";
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement