I am a new self-leaner in Haskell. firstly, I want to write a function to check if two lists of tuples are equal. Each tuple has a key and value
secondly, I want a function to union two lists of tuples
I tried several ways and tried many times but seems couldn't meet my requirements. could anyone help me? thanks in advance.
Since a
is only a member of Eq
, sorting or grouping is not an option.
import Data.List(nub, (\\))
import Data.Monoid(getSum)
type Times = Int
type Lis a = [(a,Times)]
lisEqual :: Eq a => Lis a -> Lis a -> Bool
lisEqual xs xs' = length xs == length xs' && xs \\ xs' == []
lisSum :: Eq a => Lis a-> Lis a-> Lis a
lisSum xs xs' = fmap f $ getKeys l
where
f x = (,) x (getSum . foldMap (pure . snd) . filter ((x ==) . fst) $ l)
l = xs ++ xs'
getKeys = nub . fst . unzip
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With