
Weird request: For a certain application, the "nub" function from Data.List is exactly what I need... almost. Nub removes duplicates, keeping the /first/ occurrence of each element. However, I need a function that removes duplicates, keeping the /second/ occurrence of each element. There wouldn't happen to be a function already that does this, would there? I'm working with a custom type which is a member of Eq, but some of the data in the type is not used in the comparison. So it is important which of the two "duplicates" are actually saved. -- frigidcode.com theologia.indicium.us

On 06/03/2011 11:37 PM, Christopher Howard wrote:
Weird request: For a certain application, the "nub" function from Data.List is exactly what I need... almost. Nub removes duplicates, keeping the /first/ occurrence of each element. However, I need a function that removes duplicates, keeping the /second/ occurrence of each element. There wouldn't happen to be a function already that does this, would there?
I'm working with a custom type which is a member of Eq, but some of the data in the type is not used in the comparison. So it is important which of the two "duplicates" are actually saved.
I should qualify this: By "second" I mean "last". Of course, it is possible for there to be more than one duplicate of any given element in a list. E.g., if there are three identical elements in a list, I want the third one, not the second one. -- frigidcode.com theologia.indicium.us

I would probably just use Data.Set, insert all the elements in the list into
it, and then reconvert to a list. Done. Can provide code it you need it.
On 4 June 2011 08:49, Christopher Howard
On 06/03/2011 11:37 PM, Christopher Howard wrote:
Weird request: For a certain application, the "nub" function from Data.List is exactly what I need... almost. Nub removes duplicates, keeping the /first/ occurrence of each element. However, I need a function that removes duplicates, keeping the /second/ occurrence of each element. There wouldn't happen to be a function already that does this, would there?
I'm working with a custom type which is a member of Eq, but some of the data in the type is not used in the comparison. So it is important which of the two "duplicates" are actually saved.
I should qualify this: By "second" I mean "last". Of course, it is possible for there to be more than one duplicate of any given element in a list. E.g., if there are three identical elements in a list, I want the third one, not the second one.
-- frigidcode.com theologia.indicium.us
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Howard wrote:
On 06/03/2011 11:37 PM, Christopher Howard wrote:
Weird request: For a certain application, the "nub" function from Data.List is exactly what I need... almost. Nub removes duplicates, keeping the /first/ occurrence of each element. However, I need a function that removes duplicates, keeping the /second/ occurrence of each element. There wouldn't happen to be a function already that does this, would there?
I'm working with a custom type which is a member of Eq, but some of the data in the type is not used in the comparison. So it is important which of the two "duplicates" are actually saved.
I should qualify this: By "second" I mean "last". Of course, it is possible for there to be more than one duplicate of any given element in a list. E.g., if there are three identical elements in a list, I want the third one, not the second one.
In that case, can't you simply revert the list, nub it, then revert again (i.e. reverse . nub . reverse)? regards, iustin

On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Howard wrote:
On 06/03/2011 11:37 PM, Christopher Howard wrote:
Weird request: For a certain application, the "nub" function from Data.List is exactly what I need... almost. Nub removes duplicates, keeping the /first/ occurrence of each element. However, I need a function that removes duplicates, keeping the /second/ occurrence of each element. There wouldn't happen to be a function already that does this, would there?
I'm working with a custom type which is a member of Eq, but some of the data in the type is not used in the comparison. So it is important which of the two "duplicates" are actually saved.
I should qualify this: By "second" I mean "last". Of course, it is possible for there to be more than one duplicate of any given element in a list. E.g., if there are three identical elements in a list, I want the third one, not the second one.
In that case, can't you simply revert the list, nub it, then revert again (i.e. reverse . nub . reverse)?
regards, iustin
This is a a much better solution... mine of course discards the ordering in
On 4 June 2011 09:20, Iustin Pop
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 06/04/2011 12:20 AM, Iustin Pop wrote:
On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Howard wrote:
On 06/03/2011 11:37 PM, Christopher Howard wrote:
Weird request: For a certain application, the "nub" function from Data.List is exactly what I need... almost. Nub removes duplicates, keeping the /first/ occurrence of each element. However, I need a function that removes duplicates, keeping the /second/ occurrence of each element. There wouldn't happen to be a function already that does this, would there?
I'm working with a custom type which is a member of Eq, but some of the data in the type is not used in the comparison. So it is important which of the two "duplicates" are actually saved.
I should qualify this: By "second" I mean "last". Of course, it is possible for there to be more than one duplicate of any given element in a list. E.g., if there are three identical elements in a list, I want the third one, not the second one.
In that case, can't you simply revert the list, nub it, then revert again (i.e. reverse . nub . reverse)?
regards, iustin
@iustin: Thanks, that should work well enough. @Edwards: I'm not familiar with Data.Set, so I do not understand your solution. -- frigidcode.com theologia.indicium.us

Also, (since I believe that sort is stable), (map last . group . sort) should work, and may have different performance characteristics (but requires an Ord constraint). On Sat, Jun 4, 2011 at 12:52 PM, Christopher Howard < christopher.howard@frigidcode.com> wrote:
On 06/04/2011 12:20 AM, Iustin Pop wrote:
On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Howard wrote:
On 06/03/2011 11:37 PM, Christopher Howard wrote:
Weird request: For a certain application, the "nub" function from Data.List is exactly what I need... almost. Nub removes duplicates, keeping the /first/ occurrence of each element. However, I need a function that removes duplicates, keeping the /second/ occurrence of each element. There wouldn't happen to be a function already that does this, would there?
I'm working with a custom type which is a member of Eq, but some of the data in the type is not used in the comparison. So it is important which of the two "duplicates" are actually saved.
I should qualify this: By "second" I mean "last". Of course, it is possible for there to be more than one duplicate of any given element in a list. E.g., if there are three identical elements in a list, I want the third one, not the second one.
In that case, can't you simply revert the list, nub it, then revert again (i.e. reverse . nub . reverse)?
regards, iustin
@iustin: Thanks, that should work well enough.
@Edwards: I'm not familiar with Data.Set, so I do not understand your solution.
-- frigidcode.com theologia.indicium.us
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Alex R
participants (4)
-
Alex Rozenshteyn
-
Benjamin Edwards
-
Christopher Howard
-
Iustin Pop