module Golf where
+import qualified Data.Map as M
+import qualified Data.List as L
+
-- exercise 1
skips :: [a] -> [[a]]
skips xs = [map snd $ filter (\(i, _) -> (i `mod` n) == 0) $ zip [1..] xs | n <- [1 .. length xs]]
localMaxima' :: [Integer] -> [Integer]
localMaxima' xs = [b | [a,b,c] <- [drop (snd pair) (fst pair) | pair <- zip [[ p | p <- fst (splitAt n xs)] | n <- [3..length xs]] [0..]], b > a && b > c]
+frequency :: [Integer] -> [(Integer, Integer)]
+frequency xs = M.toList $ M.fromListWith (+) ([(c,1) | c <- xs] ++
+ [(c,0) | c <- [0..9]])
+numStars :: Integer -> String
+numStars n = take (fromInteger n) (repeat '*')
+
+numSpaces :: Integer -> String
+numSpaces n = take (fromInteger n) (repeat ' ')
+
+histogram' xs = let freqs = [b | (_,b) <- frequency xs]
+ m = maximum freqs
+ in
+ reverse $ L.transpose $ [take (fromInteger m) (numStars b ++ numSpaces (10 - b)) | b <- freqs]
+
+histogram :: [Integer] -> String
+histogram xs = (L.concat $ L.intersperse "\n" $ histogram' xs) ++ "\n"
+ ++ replicate 10 '=' ++ "\n" ++ (L.concat $ map show [0..9]) ++ "\n"