From: Ramakrishnan Muthukrishnan Date: Fri, 19 Dec 2014 14:10:07 +0000 (+0530) Subject: histogram -- naive implementation X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/file/URI:LIT:krugkidfnzsc4/@@named=/cyclelanguage?a=commitdiff_plain;h=f78dad3ddf715811a774c90954b05cf41a07b03d;p=yorgey.git histogram -- naive implementation --- 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"