I have a list of lists, and a function that returns the list with the most items:
extract ::[Plateau]->Plateau
extract(x:xs)
|x==maximumBy(compare `on` length)(x:xs)=x
|otherwise = extract(xs)
extract []=[""]
Now I need a function to take the same [Plateau] and return a new [Plateau] that has the previous largest removed:
prune::[Plateau]->[Plateau]
prune(x:xs)
|x < (maximumBy(compare `on` length)(x:xs)=x : prune (xs)
|x>=maximumBy(compare `on` length)(x:xs)=[]
prune [] = []
I also call prune with a sortBy as to make sure the largest list is last:
(extract . prune) (sortBy(compare `on` length)(plateaus))
This starts out to work correctly. my List plateaus looks like:
plateaus = ["01000"], ["01010", "11010", "10010"] ["00101"], ["01101", "01001"]]
here it is sorted:
[["01000"], ["00101"], ["01101", "01001"], ["01010", "11010", "10010"]]
Now, my function prune is returning a list of
[["01000"], ["00101"]]
that tells me, for some reason Haskell thinks
["01101", "01001"] >= ["01010", "11010", "10010"]
When I can clearly see that 2 >= 3 is not true.
Why is this?
List comparison is lexicographic, not by length. So you get the result you see because “01101” >= “01010”, which in turn is for the same reason – the first two characters of the two strings are equal and the third character of the first is greater than the third character of the second.