On Sat, 2 Oct 1999, Matt Harden wrote:
[snip]
>
> I like that, but I wish we had a way to get the "head" and "tail" of
> tuples, just as we do with lists, and combine them. Maybe a (:,)
> operator that works like this:
>
> a :, (b :, ()) = (a,b)
> a :, () = UniTuple a
> a :, (b,c) = (a,b,c)
> a :, (UniTuple b) = (a,b)
>
> Also allow pattern matching on this operator.
>
[snip]
>
> This seems a little too obvious to me. Has it been suggested and shot
> down before?
>
> Matt Harden
>
Well, you can define a class Splittable:
class Splittable a b c where
spl :: a -> (b,c) -- split tuple
lps :: (b,c) -> a -- reverse split
With fairly obvious instances for Splittable a a (), Splittable (a,b) a b,
Splittable (a,b,c) a (b,c) etc. The only problem with this kind of solution
is that when you type, e.g. spl (3,4,5), hoping to obtain (3,(4,5)), hugs
complains with:
ERROR: Unresolved overloading
*** Type : (Num a, Num b, Num c, Splittable (c,b,a) d e) => (d,e)
*** Expression : spl (3,4,5)
However, it seems that Mark Jones has an extension to Hugs in the works
where you can specify that the types b and c in the class depend on a,
which would resolve this issue. See http://www.cse.ogi.edu/~mpj/fds.html
for details - I really hope the September release comes quickly !!!
Bye,
Jan de Wit