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

Paul Bone paul at bone.id.au
Mon Nov 23 09:48:35 AEDT 2015


On Fri, Nov 20, 2015 at 07:29:38PM +1100, Julien Fischer wrote:
> 
> On Fri, 20 Nov 2015, Paul Bone wrote:
> 
> >>
> >>:- 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.

People are going to write code that expresses what they want, not how the
language implementation expects them to write so that it avoids a quadratic
behaviour that they may not even understand or know about.

https://www.facebook.com/GreenSetGo/photos/a.322584721161331.79328.322573727829097/893772377375893/?type=3&theater

They might later test it for performance and make changes, but few
developers will do this.

I didn't benchmark this 99 bottles of beer program.  But it appears to run
"fast enough" (for N=99).

-- 
Paul Bone



More information about the reviews mailing list