[erlang-questions] Sets from list then to list? (found it)

Alexander Lamb <>
Mon Apr 7 12:35:13 CEST 2008


Sorry for replying too fast. I look into the code of usort and found  
that to be equal you need A < B and B < A to be true (which was not  
the case in my function...)

THanks,

Alex
Le 7 avr. 08 à 11:57, Alexander Lamb a écrit :
> Well, spoke to fast. usort/1 works fine, but when I provide my own
> function, it doesn't remove the duplicates.
> It looks like this is because my sort function can only return true or
> false. What should it return to usort/2 removes correctly duplicates.
>
> Thanks,
>
> Alex
>
> Le 7 avr. 08 à 11:18, Alexander Lamb a écrit :
>> Thanks, usort(Fun, List) is the solution!
>>
>> (I need to sort according to my own algorithm)
>>
>> Alex
>>
>> Le 7 avr. 08 à 11:07, Hynek Vychodil a écrit :
>>>
>>>
>>> On Mon, Apr 7, 2008 at 10:51 AM, Kostis Sagonas <>
>>> wrote:
>>> Alexander Lamb wrote:
>>>> Hello (list:-)
>>>>
>>>> Using list comprehension, I am building a list of tuples that I  
>>>> want
>>>> unique and sorted.
>>>>
>>>> Is this a correct way of doing it:
>>>>
>>>> Result = sets:to_list(sets:from_list([ {A,B} || {_X,_Y,A,_Z,B} <-
>>>> MyList]))
>>>>
>>>> Then sort the Result.
>>>>
>>>> The alternative would be to use one of the lists functions to copy
>>>> elements into a new list and testing first at each insert if the
>>>> element is already present.
>>>>
>>>> What is best?
>>>
>>> None of the two.  Check out lists:usort/1.
>>>
>>> 1> lists:usort([{1,2},{1,3},{0,1},{1,2}]).
>>> [{0,1},{1,2},{1,3}]
>>>
>>> Yup, faster one :-)
>>>
>>>
>>> Kostis
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>
>>>
>>>
>>> -- 
>>> --Hynek (Pichi) Vychodil
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>




More information about the erlang-questions mailing list