Skip to content

Instantly share code, notes, and snippets.

@redbug312
Last active May 6, 2018 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redbug312/2a0ae0f95cfc42a41f5476913a2db215 to your computer and use it in GitHub Desktop.
Save redbug312/2a0ae0f95cfc42a41f5476913a2db215 to your computer and use it in GitHub Desktop.
FLOLAC'18 prerequisite p2
stat :: [Int] -> [Int]
stat [] = replicate 10 0
stat (x:xs) = (take x stats) ++ [stats !! x + 1] ++ tail (drop x stats)
where stats = stat xs
plot :: [Int] -> [String]
plot ys
| all (== 0) ys = []
| otherwise = row : plot ys'
where row = map (\y -> if y == 0 then ' ' else '*') ys
ys' = map (\y -> max 0 $ y - 1) ys
histogram :: [Int] -> String
histogram xs = unlines $ reverse $ "0123456789" : "==========" : plot (stat xs)
{-
ghci> putStr $ histogram [1,4,5,4,6,6,3,4,2,4,9]
*
*
* *
****** *
==========
0123456789
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment