Last active
August 29, 2015 14:11
-
-
Save Ikke/933cecfcf01d2ec49b5f to your computer and use it in GitHub Desktop.
Creates a vertical histogram of numbers
This file contains hidden or 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
import Data.List | |
histogram :: [Int] -> String | |
histogram xs = | |
let c = countNumbers xs | |
h = foldr max 0 c | |
l = "==========" | |
n = "0123456789" | |
g = unlines $ reverse $ transpose $ numbersToStars h c | |
in g ++ l ++ "\n" ++ n | |
countN :: Int -> [Int] -> Int | |
countN n xs = length $ filter (== n) xs | |
countNumbers :: [Int] -> [Int] | |
countNumbers xs = map (\n -> countN n xs) [0..9] | |
repeatl :: Int -> x -> [x] | |
repeatl l w = take l $ repeat w | |
numbersToStars :: Int -> [Int] -> [String] | |
numbersToStars h (x:xs) = (repeatl x '*' ++ repeatl (h-x) ' ') : numbersToStars h xs | |
numbersToStars _ [] = [] |
This file contains hidden or 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
* | |
* | |
* | |
* | |
* * | |
** ** | |
********* | |
========== | |
0123456789 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment