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

Julien Fischer jfischer at opturion.com
Fri Nov 20 19:29:38 AEDT 2015



On Fri, 20 Nov 2015, Paul Bone wrote:

> On Fri, Nov 20, 2015 at 05:48:36PM +1100, Julien Fischer wrote:
>>
>> Hi Paul,
>>
>> On Fri, 20 Nov 2015, Paul Bone wrote:
>>
>>> 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.
>>
>> Oooh, can I play this game too?  If your aim is to provide a more
>> idiomatic version, I'd suggest:
>>
>> - not using explicit recursion where the standard library already
>>   provides a procedure (int.fold_down) that does the same thing.
>> - use if-then-else rather that (C->T;E)
>> - there's lots of string appending using ++ going on in your version, I'd
>>   call append_list instead.
>>
>> I've attached an alternative  version.
>
> Feel free to update it.

Actually, here's an even smaller version using int.fold_down.

...

>>         bottles(N - 1, " on the wall.\n\n", !IO)
>>     ).
>>
>> :- pred bottles(int::in, string::in, io::di, io::uo) is det.
>>
>> bottles(N, T, !IO) :-
>>     ( if N = 0 then
>>         io.format("No more bottles of beer%s", [s(T)], !IO)
>>     else if N = 1 then
>>         io.format("1 bottle of beer%s", [s(T)], !IO)
>>     else
>>         io.format("%d bottles of beer%s", [i(N), s(T)], !IO)
>>     ).
>
> Why pass the IO state through everything?

In this case?  Because everything is doing I/O. ;-)

> I try to avoid this where conveinent.

In this case I can't see a good reason to do so.

> I think append_list would have been better, it avoids the
> repeated string appends

Either append_list or string.format would have been better than
what you have.

Julien.
-------------- next part --------------
% vim: ts=4 et ft=mercury

:- module beer4.
:- interface.

:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module int.
:- import_module list.
:- import_module string.

main(!IO) :-
    int.fold_down(beer, 1, 99, !IO),
    io.print_line("Go to the store and buy some more!", !IO).

:- pred beer(int::in, io::di, io::uo) is det.

beer(N, !IO) :-
    bottles(N, " on the wall,\n", !IO),
    bottles(N, ".\nTake one down, pass it around,\n", !IO),
    bottles(N - 1, " on the wall.\n\n", !IO).

:- pred bottles(int::in, string::in, io::di, io::uo) is det.

bottles(N, T, !IO) :-
    ( if N = 0 then
        io.format("No more bottles of beer%s", [s(T)], !IO)
    else if N = 1 then
        io.format("1 bottle of beer%s", [s(T)], !IO)
    else
        io.format("%d bottles of beer%s", [i(N), s(T)], !IO)
    ).


More information about the reviews mailing list