diff options
| author | Peter Hull <[email protected]> | 2010-09-20 22:08:28 +0100 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2010-09-30 13:50:25 -0700 |
| commit | f6e3e6903b20df01a6abe36d741c39d6e9696ebb (patch) | |
| tree | 0a6754a03a66878aa0b4567e5b1420c5a006d785 /src/test/bench/99-bottles/99bob-simple.rs | |
| parent | implemented break for while-loop case (diff) | |
| download | rust-f6e3e6903b20df01a6abe36d741c39d6e9696ebb.tar.xz rust-f6e3e6903b20df01a6abe36d741c39d6e9696ebb.zip | |
Initial check-in of 99 Bottles Of Beer
using different methods (simple, iterator, tail-call, pattern match)
Diffstat (limited to 'src/test/bench/99-bottles/99bob-simple.rs')
| -rw-r--r-- | src/test/bench/99-bottles/99bob-simple.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/test/bench/99-bottles/99bob-simple.rs b/src/test/bench/99-bottles/99bob-simple.rs new file mode 100644 index 00000000..10fec27a --- /dev/null +++ b/src/test/bench/99-bottles/99bob-simple.rs @@ -0,0 +1,61 @@ +/* -*- mode:rust;indent-tabs-mode:nil -*- + * Implementation of 99 Bottles of Beer + * http://99-bottles-of-beer.net/ + */ +use std; +import std._int; +import std._str; + +fn b1() -> str { + ret "# of beer on the wall, # of beer."; +} +fn b2() -> str { + ret "Take one down and pass it around, # of beer on the wall."; +} +fn b7() ->str { + ret "No more bottles of beer on the wall, no more bottles of beer."; +} +fn b8() -> str { + ret "Go to the store and buy some more, # of beer on the wall."; +} + +fn sub(str t, int n) -> str { + let str b = ""; + let uint i = 0u; + let str ns; + alt (n) { + case (0) { + ns = "no more bottles"; + } +case (1) { + ns = "1 bottle"; + } + case (_) { + ns = _int.to_str(n, 10u) + " bottles"; + } + } + while (i < _str.byte_len(t)) { + if (t.(i) == ('#' as u8)) { + b += ns; + } + else { + b += t.(i); + } + i += 1u; + } + ret b; +} + +/* Straightforward counter */ +fn main() { + let int n=99; + while (n > 0) { + log sub(b1(), n); + log sub(b2(), n - 1); + log ""; + n -= 1; + } + log b7(); + log sub(b8(),99); +} + |