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

Ozgur Akgun ozgurakgun at gmail.com
Tue Sep 28 06:24:29 EDT 2010

I'll first try to give you a more understandable syntax. (I hope.)

nubSym :: Ord a => [(a,a)] -> [(a,a)]
nubSym xs = nub (map fix xs)
  where fix (a,b) | a > b     = (b,a)
                  | otherwise = (a,b)

The two changes above are getting rid of the point-free style[1] and trading
the pattern matching syntax for guards[2].
In the first version the p was for matching with any parameter that didn't
match the first definition. You may want to have a look at how pattern
matching in Haskell works. (Well, you can find lots of resources about this
one but I would have a look at [3])

[1] http://www.haskell.org/haskellwiki/Pointfree
[2] http://en.wikibooks.org/wiki/Haskell/Control_structures#Guards
[3] http://learnyouahaskell.com/syntax-in-functions

happy hacking! :)

On 28 September 2010 11:14, Martin Tomko <martin.tomko at geo.uzh.ch> wrote:

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

Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100928/7061a752/attachment.html

More information about the Beginners mailing list