[m-dev.] for review: tuples [1]

Simon Taylor stayl at cs.mu.OZ.AU
Wed Aug 2 09:37:10 AEST 2000


Ralph Becket wrote:
> p.s. Here's a slightly contrived example of where one
> might prefer to use tuples.  The alternative is to use
> preds, which would be a bit more cumbersome IMHO.
> 
> :- func minmax(list(T)) = {T, T}. % {min, max}
> 
> minmax([]) = throw("minmax/1: empty list!").
> 
> minmax([X | Xs]) =
> 	list__foldl(
> 		( func(Y, {Min, Max}) =
> 			{(if Y < Min then Y else Min),
> 			 (if Max < Y then Y else Max)}
> 		),
> 		Xs,
> 		{X, X}
> 	).
> 
> p.p.s. The obvious pred version using list__foldl2
> doesn't do any structure creation, whereas the func
> version above would appear to, generating a new
> tuple on each iteration.  Is the deforestation
> optimisation up to getting rid of that small
> inefficiency in my example?  Or perhaps compile-time
> GC would do the trick?

This isn't really something that deforestation was designed
to deal with. Deforestation attempts to remove multiple
traversals over a data structure.

We've discussed this kind of optimization before. It's
never been implemented, mainly because it's difficult
to predict when it will improve performance.

Structure reuse would work on this function, but
that isn't likely to happen for a while.

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