Flolac week of code 2: histogram
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
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