Reputation: 323
So I would like to use groupBy
to group my list of tuples based on their snd
element. Every tuple with the same snd
element should be in the same list
group :: [(Int,Int)] -> [[(Int,Int)]]
group = groupBy (\a b -> snd a == snd b) lijst
A groupBy
of the list [(1,2),(8,9),(5,2),(9,2),(3,9),(1,1)]
should return
[[(1,2),(5,2),(9,2)],[(8,9),(3,9)],[1,1]]
.
However it returns [[(1,2)],[(8,9)],[(5,2),(9,2)],[(3,9)],[(1,1)]]
.
How can I make this function work ?
Upvotes: 1
Views: 576
Reputation: 4867
Reading in the documentation of groupBy we are referred to the documentation of group, which states
The group function takes a list and returns a list of lists such that the concatenation of the result is equal to the argument.
That's a not very obvious way of saying: "We only group equal, adjacent elements."
So sortBy your list first.
Upvotes: 6