[m-rev.] diff: Add 99 bottles of beer sample

Paul Bone paul at bone.id.au
Fri Nov 20 16:57:44 AEDT 2015


Add 99 bottles of beer sample

samples/beer.m
    Add this sample.

    I found the original version of this sample on 99-bottles-of-beer.net.
    I've updated it, _tried_ to post a comment to 99-bottles-of-beer.net,
    added it to rosetta code and thought, being a small example, that it
    should also belong here.
---
 samples/beer.m | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 samples/beer.m

diff --git a/samples/beer.m b/samples/beer.m
new file mode 100644
index 0000000..e0b2d27
--- /dev/null
+++ b/samples/beer.m
@@ -0,0 +1,61 @@
+% file: beer.m
+% author:
+%   Fergus Henderson <fjh at cs.mu.oz.au> Thursday 9th November 1995
+% Re-written with new syntax standard library calls:
+%   Paul Bone <paul at mercurylang.org> 2015-11-20
+%
+% This beer song is more idiomatic Mercury than the original, I feel bad
+% saying that since Fergus is a founder of the language.
+
+:- module beer.
+:- interface.
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module int.
+:- import_module list.
+:- import_module string.
+
+main(!IO) :-
+    beer(99, !IO).
+
+:- pred beer(int::in, io::di, io::uo) is det.
+
+beer(N, !IO) :-
+    io.write_string(beer_stanza(N), !IO),
+    ( N > 0 ->
+        io.nl(!IO),
+        beer(N - 1, !IO)
+    ;
+        true
+    ).
+
+:- func beer_stanza(int) = string.
+
+beer_stanza(N) = Stanza :-
+    ( N = 0 ->
+        Stanza = "Go to the store and buy some more!\n"
+    ;
+        NBottles = bottles_line(N),
+        N1Bottles = bottles_line(N - 1),
+        Stanza =
+            NBottles ++ " on the wall.\n" ++
+            NBottles ++ ".\n" ++
+            "Take one down, pass it around,\n" ++
+            N1Bottles ++ " on the wall.\n"
+    ).
+
+:- func bottles_line(int) = string.
+
+bottles_line(N) =
+    ( N = 0 ->
+        "No more bottles of beer"
+    ; N = 1 ->
+        "1 bottle of beer"
+    ;
+        string.format("%d bottles of beer", [i(N)])
+    ).
+
-- 
2.6.2




More information about the reviews mailing list