2011-06-15 11:19:50 -07:00
|
|
|
|
|
|
|
|
2011-07-13 15:44:09 -07:00
|
|
|
/* -*- mode::rust;indent-tabs-mode::nil -*-
|
2010-09-20 22:08:28 +01:00
|
|
|
* Implementation of 99 Bottles of Beer
|
|
|
|
* http://99-bottles-of-beer.net/
|
|
|
|
*/
|
|
|
|
use std;
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::int;
|
|
|
|
import std::str;
|
2010-09-20 22:08:28 +01:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
tag bottle { none; dual; single; multiple(int); }
|
2010-09-20 22:08:28 +01:00
|
|
|
|
|
|
|
fn show(bottle b) {
|
2011-06-15 11:19:50 -07:00
|
|
|
alt (b) {
|
|
|
|
case (none) {
|
|
|
|
log "No more bottles of beer on the wall, " +
|
|
|
|
"no more bottles of beer,";
|
|
|
|
log "Go to the store and buy some more, " +
|
|
|
|
"99 bottles of beer on the wall.";
|
|
|
|
}
|
|
|
|
case (single) {
|
|
|
|
log "1 bottle of beer on the wall, 1 bottle of beer,";
|
|
|
|
log "Take one down and pass it around, " +
|
|
|
|
"no more bottles of beer on the wall.";
|
|
|
|
}
|
|
|
|
case (dual) {
|
|
|
|
log "2 bottles of beer on the wall, 2 bottles of beer,";
|
|
|
|
log "Take one down and pass it around, " +
|
|
|
|
"1 bottle of beer on the wall.";
|
|
|
|
}
|
|
|
|
case (multiple(?n)) {
|
|
|
|
let str nb = int::to_str(n, 10u);
|
|
|
|
let str mb = int::to_str(n - 1, 10u);
|
|
|
|
log nb + " bottles of beer on the wall, " + nb +
|
|
|
|
" bottles of beer,";
|
|
|
|
log "Take one down and pass it around, " + mb +
|
|
|
|
" bottles of beer on the wall.";
|
|
|
|
}
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-09-20 22:08:28 +01:00
|
|
|
fn next(bottle b) -> bottle {
|
2011-06-15 11:19:50 -07:00
|
|
|
alt (b) {
|
|
|
|
case (none) { ret none; }
|
|
|
|
case (single) { ret none; }
|
|
|
|
case (dual) { ret single; }
|
|
|
|
case (multiple(3)) { ret dual; }
|
|
|
|
case (multiple(?n)) { ret multiple(n - 1); }
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
|
|
|
|
2010-09-20 22:08:28 +01:00
|
|
|
// Won't need this when tags can be compared with ==
|
|
|
|
fn more(bottle b) -> bool {
|
2011-06-15 11:19:50 -07:00
|
|
|
alt (b) { case (none) { ret false; } case (_) { ret true; } }
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-09-20 22:08:28 +01:00
|
|
|
fn main() {
|
2011-06-15 11:19:50 -07:00
|
|
|
let bottle b = multiple(99);
|
|
|
|
let bool running = true;
|
|
|
|
while (running) { show(b); log ""; running = more(b); b = next(b); }
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|