[m-dev.] Adding Loops to Mercury

Paul Bone paul at bone.id.au
Thu Jan 2 22:09:15 AEDT 2014


On Thu, Jan 02, 2014 at 07:30:18PM +1100, Julien Fischer wrote:
> On Thu, Jan 2, 2014 at 7:26 PM, Paul Bone <paul at bone.id.au> wrote:
> 
> > On Thu, Jan 02, 2014 at 06:20:27PM +1100, Julien Fischer wrote:
> > > On Thu, Jan 2, 2014 at 5:50 PM, Michael Day <mikeday at yeslogic.com>
> > wrote:
> > >
> > > > Hi Paul,
> > > >
> > > >
> > > >  foreach I=std::fromTo(1,1000) do
> > > >>>     %...whatever
> > > >>> end foreach.
> > > >>>
> > > >>
> > > >> I think that such a feature would be considered "not in the spirit of
> > > >> Mercury" by most developers.
> > > >>
> > > >
> > > > Light syntax and efficient compilation for lambdas is enough:
> > > >
> > > >     foreach(1 `..` 100, func(X) = blah)
> > >
> > >
> > > Which is pretty much already supported, e.g.
> > >
> > >       map(func(X) = X * 2, 1 `..` 100)
> > >
> > > (Use a quantifier like foreach seems like the wrong name to use
> > > for some that is transform the list of integers using a function.)
> >
> > foreach would be a predicate that is very similar to list.foldl.
> >
> 
> It wouldn't, you would need to provide the initial value of the accumulator
> somewhere.  In any case, my point was that foreach is not a good name
> for such a thing.

We do that in list.foldl as a parameter.  I don't see why it should be any
different.  Anyway, I'm not saying that Mercury should add foreach anywhere.
I'm saying that anyone who wants to create loops can write a wrapper around
foldl or anything else to create the abstractions they want.  Or just use
foldl because it is already such an abstraction.


-- 
Paul Bone
http://www.bone.id.au



More information about the developers mailing list