[Haskell-beginners] removing duplicate tuples (including symmetrical ones)

Martin Tomko martin.tomko at geo.uzh.ch
Tue Sep 28 06:14:27 EDT 2010


Hi Ozgur,
well, I am getting a list of tuples from a previous function, and they 
relate to edges in graphs, so I am not too keen to change that, although 
that could be possible. But I never worked with sets in Haskell, so will 
have to study.

Regarding your suggestion - I have to study it, it is a bit advanced. 
First, I see there is no paramter to nubSym  - I have never used that 
syntax, shouldn't there be something like nymSym (x:xs) or so?
Second, obviously there is a local function, fix. I understand this: fix 
(a,b) | a > b = (b,a)
but I am not sure how to interpret this:
fix p = p. Where does p come from? How does haskell know that it relates 
to (a,b), or the x as parameter?

Just asking for clarification ,as I am new to all this.

Thanks
M.

On 9/28/2010 12:05 PM, Ozgur Akgun wrote:
> Hi,
>
> On 28 September 2010 10:33, Martin Tomko <martin.tomko at geo.uzh.ch 
> <mailto: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
>
> Cheers,
> Ozgur


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100928/309f6c17/attachment.html


More information about the Beginners mailing list