[m-dev.] Re: The Great Computer Language Shootout

Ralph Becket rbeck at microsoft.com
Wed Jan 10 21:04:41 AEDT 2001


>From Robert Jeschofnik on 10/01/2001 05:39:48
> > 
> > A predicate that I seem to have written too many times is:
> > 
> > :- pred for(int, int, pred(int, T, T), T, T).
> 
> I thought this actually had been added. I remember that it was
> mentioned to be a good idea when I first posted your curses stuff for
> inclusion in the extras.

I'd vote (a) for having a function rather than a predicate and 
(b) for a name other than `for' so we don't clash with the forthcoming
sequence quantification facility which has already laid claim to the
name.

How about this (I've tried to copy familiar C for(;;) argument
ordering, although that may not be the best idea):

:- func iterate(T, pred(T), func(T) = T, func(T, X) = X, X) = X.
:- mode iterate(in, pred(in) is semidet, func(in) = out is det,
		func(in, in) = out is det, in) = out is det.

iterate(I, ContP, Succ, Fn, X) =
	( if ContP(I) then
		iterate(Succ(I), ContP, Succ, Fn, Fn(I, X))
	  else
		X
	).

and for the integer range case...

:- func over_range(int, int, func(int, X) = X, X) = X.

over_range(Lo, Hi, Fn, X) =
	iterate(Lo, (>=)(Hi), plus(1), Fn, X).

I'd be inclined to put these in std_util.

Ralph

--
Ralph Becket      |      MSR Cambridge      |      rbeck at microsoft.com 

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list