I am having trouble figuring out how to reverse a tuple, below is my code:
extractCode :: HTree -> HCodeMap
extractCode t = extractCodeInner t []
where extractCodeInner (Leaf _ c) bits = [(c, [])]
extractCodeInner (Branch _ left right) bits = map (addBit Zero) (extractCode left ) ++ map (addBit One) (extractCode right)
where addBit b = second $ ( b : )
Where HTree is defined as an Int (The frequency of a character occurring in a String) and Char from the String:
data HTree = Branch Int HTree HTree | Leaf Int Char
deriving (Show, Eq)
and HCodeMap is:
type HCodeMap = [(Char, [Bit])]
Where the Char is a character from the tree and [Bit] is:
data Bit = Zero | One deriving (Show, Eq)
Now when I pass the tree of the string "aaabbc"
I get:
[('c',[Zero, Zero]),('b',[Zero, One]),('a',[One])]
What I would like to do is reverse all of the items in the round bracket list (), such that it will look like this:
[('a',[One]),('b',[Zero, One]),('c',[Zero, Zero])]
I tried to using the sort . reverse I found in this article but I'm not sure how to https://codegolf.stackexchange.com/questions/3571/descending-sort-using-standard-ascending-sort-function
And I previously used a sortby( compare 'on' snd) function but trying to change this to reverse isn't that easy to work through in my head.
I am relatively new to Haskell, so I am sorry if this is a stupid question. Its just really easy to do on a normal list but I can't visualize how I would do it on a tuple.
Thank you in advance!
\xs -> let (a,b) = unzip xs in zip a (reverse b)
– user2407038