I'm writing a function pad
that takes a list and pads it until it is a certain size. I tried 2 implementations:
pad :: Monoid a => Int -> [a] -> [a]
pad len list = replicate (len - length list) mempty ++ list
and
pad :: Int -> a -> [a] -> [a]
pad len value list = replicate (len - length list) value ++ list
The first one seems to be a logical usage of Monoid
but calling it with lists of integers (or anything that is a Monoid
in multiple ways) is a pain:
(fmap getSum) <$> pad 8 <$> (fmap Sum) <$> [1,2,3]
I don't really mind the extra typing, but it doesn't even seem to convey the meaning very well. How would you implement this function?