[Haskell-beginners] removing duplicate tuples (including
ozgurakgun at gmail.com
Tue Sep 28 06:05:14 EDT 2010
On 28 September 2010 10:33, Martin Tomko <martin.tomko at geo.uzh.ch> wrote:
> I have a list of (a,a) tuples, and am trying something like nub, but also
> matching for symmetrical tuples.
You can of course do this. One approach would be to simply 'fix' the tuples
according to some ordering, and then use standard nub - or a better one.
But to me, the real question is this: If the order of your tuples to don't
matter, do you actually need tuples? There are other types in which the
order of the elements in a container does not change the meaning; such as a
set. You may want to use a Set from Data.Set, or you can define a pair type
in which ordering doesn't matter. It will end up being a cardinality
restricted set type though.
If you just want to get it working, here is some code for the first option:
nubSym :: Ord a => [(a,a)] -> [(a,a)]
nubSym = nub . map fix
where fix (a,b) | a > b = (b,a)
fix p = p
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners