FLOLAC 2018 Warm-up Week 2 -- histogram: make a histogram from a list of [0..9]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numOccs :: [Int] -> [Int] | |
numOccs xs = map (\n -> length $ filter (== n) xs) [0..9] | |
histogramLine :: [Int] -> Int -> String | |
histogramLine freqs bl = map (putc . (>= bl)) freqs | |
where putc cond = if cond then '*' else ' ' | |
histogram :: [Int] -> String | |
histogram xs = unlines $ | |
(map (histogramLine freqs) (cbTo1 $ maximum freqs) | |
++ footer) | |
where labels = "0123456789" | |
footer = [replicate (length labels) '=', labels] | |
freqs = numOccs xs | |
cbTo1 x = reverse [1 .. x] | |
{- | |
usage: putStr $ histogram [...] | |
example: | |
> 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