Reputation: 433
So, having scoured the Internet for quite some time for a nice solution, I have arrived at the end of the road.
I am currently faced with a few Haskell problems regarding text-handling, and most of it has been going perfectly fine. However, I am now stuck at sorting tuples within a list.
Basically, I have a list of tuples, so something along the lines of
list = [("hey","there"),("there","hey"),("bears","are")]
My goal now is to sort the tuples internally so that I receive
list = [("hey","there"),("hey","there"),("are","bears")]
Which I will then run a group $ sort list
on to be able to retrieve a tally of how many equivalent pairs there are. In other words, ("hey,"there")
is equivalent to ("there","hey")
.
What I have tried so far is using a map function. However, it seems that you cannot run list operations using the map function, but to at least attempt to understand what my final goal is, here is what I have achieved so far.
countup tuples =
let final =
sort sorted_internal where
sorted_internal = map (\x->(sort x)) tuples
in map (\x->(head x, length x)) final
Upvotes: 0
Views: 616
Reputation: 62818
The sort
function can't sort the elements of a tuple; it only works for lists.
You have two options. One is to write a function for sorting (2-element) tuples:
sortTuple :: (Ord a) => (a,a) -> (a,a)
sortTuple (x,y) = (min x y, max x y)
map sortTuple tuples
The other is to use lists instead of tuples:
map sort [["hey","there"], ["hey","there"], ["are","bears"]]
Upvotes: 10