Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a hard time understanding how to use nubBy

Tags:

haskell

I have a problem where i have to manipulate a list of a list of floats. [[Float]]. these list of floats are of length 4. I want to remove duplicates where the first 3 elements are tested, but ignore the 4th one. This is the last part of a multi part problem and i have been banging my head on a wall for a while figuring out how to use this. I cant find any helpful information.

fixDuplicates :: [[Float]] -> [[Float]]
fixDuplcates [[]] = [[]]
fixDuplicates x = nubBy ?
like image 763
Derek Meyer Avatar asked Nov 08 '11 05:11

Derek Meyer


1 Answers

nubBy takes a function to use for comparing elements for equality. Your definition of equality is that two lists are equal if their first three elements match. A straightforward implementation of this is:

fixDuplicates xs = nubBy firstThreeMatch xs
   where firstThreeMatch ys zs = take 3 ys == take 3 zs
like image 145
hammar Avatar answered Oct 04 '22 00:10

hammar