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

Ralph Becket rbeck at microsoft.com
Wed Aug 2 00:44:21 AEST 2000


> From: Michael Day [mailto:mcda at students.cs.mu.oz.au]
>
> > Any plans to deprecate the pair/2 type?
> 
> Could perhaps someone skilled in the use of tuples in 
> functional languages
> (Ralph?) say a few words regarding when it is more appropriate to use
> tuples and when to use pair or other similar types? Is it of 
> particular
> use for writing higher order code? I'm a little lost 
> regarding when to use
> lists, when to use terms and when to use tuples.

Tuples are useful when you want an anonymous n-vector of
heterogeneous objects.  The pair type exists because the
most common such vector is of size two.  Generalizing
tuples is much more useful.  One can only use lists for
collections of objects of the same type.  Currently, if
one needs an n > 2-vector, then one has to define a new,
named no-tag type.

I'd be happy to see pairs deprecated in favour of 2-tuples.

I think, for the most part, one should use tuples in a similar
fashion to the way one uses lambdas.  If a record structure
is pervasive or not blindingly obvious in its purpose, then
it's better to use a documented type.  Similarly, if a
function is going to be used all over the place or has a
complicated definition, it's usually better style to make 
it a named function.

> And... can a zero-arity tuple replace the unit type? For 
> completeness? :)

That's not such a bad idea.

Ralph

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?


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