From f78dad3ddf715811a774c90954b05cf41a07b03d Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Fri, 19 Dec 2014 19:40:07 +0530
Subject: [PATCH] histogram -- naive implementation

---
 hw3/Golf.hs | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/hw3/Golf.hs b/hw3/Golf.hs
index 057d284..7c6872f 100644
--- a/hw3/Golf.hs
+++ b/hw3/Golf.hs
@@ -1,5 +1,8 @@
 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]]
@@ -16,3 +19,20 @@ localMaxima (x:y:z:xs) = localMaxima [x,y,z] ++ localMaxima (y:z: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"
-- 
2.45.2