You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- FLOLAC 2018 Week 2 -- Histogramhistogram:: [Int] ->String
histogram ix =concat [ row n | n <- [heigh, heigh -1..1]] ++"==========\n"++"0123456789\n"where
row m = [ if mm < m then''else'*'| mm <- accl ] ++"\n"
accl = [ length$filter (== nn ) ix | nn <- [0..9] ]
heigh =maximum accl
-- In GHCi we should put the histogram output in 'putSrtLn' function-- In order to make "\n" actually be line feed{- e.g.putStrLn (histogram [])putStrLn (histogram [1,2,3,4,5,6,7])putStrLn (histogram [1,6,4,2,7,8,4,9,3,0,3,9,7,3])putStrLn (histogram [1,4,5,4,6,6,3,4,2,4,9])-}
histogram:: [Int] ->String
histogram = addFootter .unlines. graph . tally where
addFootter s = s ++"==========\n0123456789\n"tally:: [Int] -> [(Int, Int)]
tally = foldl' accu ary where
ary =zip [0..9] (repeat0)
tInc x t@(i, n) =if i == x then (i, n +1) else t
accu xs x =map (tInc x) xs
graph:: [(Int, Int)] -> [String]
graph xs = unfoldr row height where
height =foldr (max.snd) 0 xs
col n =map (\t ->ifsnd t >= n then'*'else'') xs
row 0=Nothing
row h =Just(col h, h -1)
toRecord:: (Int->Int) ->Int-> (Int->Int)
toRecord r x y | x == y = (r x) +1|otherwise= r y
histogram:: [Int] ->String
histogram xs =unlines (f record max) ++"==========\n0123456789\n"where count =foldl toRecord (const0) xs
record =map count [0..9]
max=maximum record
f:: [Int] ->Int-> [String]
f _ 0=[]
f record most =map (\x ->if x < most then''else'*') record : (f record (most-1))
histogram a =concat (map (++"\n") ([concat [if c!!j >= i then"*"else""| j <- [0..9]] | i <-reverse [1..maximum c]] ++ ["==========", "0123456789"]))
where c = [length (filter (==i) a) | i <- [0..9]]