Finding unique elements of a list
Thomas Lindgren
thomasl_erlang@REDACTED
Thu Dec 2 16:30:57 CET 2004
--- Sven-Olof Nystr|m <svenolof@REDACTED> wrote:
> Removing duplicates with sort assumes that < is a
> total
> ordering. Unfortunately it is not: We have neither 1
> < 1.0 nor 1.0 < 1
> Thus, trying to remove the duplicates of a list of
> floats and integers
> will give strange results.
>
> 3> tl:unique([1,1.0,1,1.0]).
> [1,1.00000,1,1.00000]
As far as I can tell from lists.erl, sort seems to use
==/2 to actually get a total ordering; since 1 == 1.0,
we get the effect of "mixed" floats and integers.
Perhaps someone closer acquainted with sort can tell
us the real story.
Rewriting drop/2 of my original program to use ==/2
instead of pattern matching yields [1.00000] for the
example above, eliminating all duplicates (as far as
==/2 is concerned). At this point, those of a
legalistic bent may perk up and begin to ask what
defines a "unique" element ... :-)
Moral of the story: stay away from floats?
Best,
Thomas
__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail
More information about the erlang-questions
mailing list