Last active
May 12, 2018 08:44
-
-
Save taiansu/c95a61da3d2dc28a8a7798cd3c1fcfee to your computer and use it in GitHub Desktop.
Flolac week of code 2: histogram
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
module Histogram where | |
import Data.List (unfoldr, foldl') | |
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] (repeat 0) | |
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 -> if snd t >= n then '*' else ' ') xs | |
row 0 = Nothing | |
row h = Just(col h, h - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment