Skip to content

Instantly share code, notes, and snippets.

@taiansu
Last active May 12, 2018 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taiansu/c95a61da3d2dc28a8a7798cd3c1fcfee to your computer and use it in GitHub Desktop.
Save taiansu/c95a61da3d2dc28a8a7798cd3c1fcfee to your computer and use it in GitHub Desktop.
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