Skip to content

Instantly share code, notes, and snippets.

@taiansu
Last active May 12, 2018 08:44
Embed
What would you like to do?
Flolac week of code 2: histogram
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