Created
May 4, 2018 02:32
-
-
Save scmu/f673586e1e4a6bc3e30a957e3cf8d4a2 to your computer and use it in GitHub Desktop.
Exercise: histogram and graph
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
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