Skip to content

Instantly share code, notes, and snippets.

@scmu
Created May 4, 2018 02:32
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 scmu/f673586e1e4a6bc3e30a957e3cf8d4a2 to your computer and use it in GitHub Desktop.
Save scmu/f673586e1e4a6bc3e30a957e3cf8d4a2 to your computer and use it in GitHub Desktop.
Exercise: histogram and graph
import Control.Arrow ((&&&))
import Data.String (unlines)
import Data.List (unfoldr,foldl')
histogram :: [Int] -> String
histogram = graph . histo
histo :: [Int] -> [Int]
histo = foldl' oplus [0,0,0,0,0,0,0,0,0,0]
where oplus xs i = zipWith ($) (replicate i id ++ [(1+)] ++ repeat id) xs
graph :: [Int] -> String
graph = unlines . reverse . ("0123456789":) . ("==========":) . unfoldr step
where step xs | all (<=0) xs = Nothing
| otherwise = Just . (map star &&& map decr) $ xs
star n = if n > 0 then '*' else ' '
decr n = if n > 0 then n-1 else 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment