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

Paul Bone paul at bone.id.au
Mon Nov 23 09:52:36 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.

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).

(Reposted with attachment rather than link.)

-- 
Paul Bone
-------------- next part --------------
A non-text attachment was scrubbed...
Name: design.jpg
Type: image/jpeg
Size: 171430 bytes
Desc: not available
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20151123/6a2a4c63/attachment.jpg>


More information about the reviews mailing list