Skip to content

Instantly share code, notes, and snippets.

@Ikke
Last active August 29, 2015 14:11
Show Gist options
  • Save Ikke/933cecfcf01d2ec49b5f to your computer and use it in GitHub Desktop.
Save Ikke/933cecfcf01d2ec49b5f to your computer and use it in GitHub Desktop.
Creates a vertical histogram of numbers
import Data.List
histogram :: [Int] -> String
histogram xs =
let c = countNumbers xs
h = foldr max 0 c
l = "=========="
n = "0123456789"
g = unlines $ reverse $ transpose $ numbersToStars h c
in g ++ l ++ "\n" ++ n
countN :: Int -> [Int] -> Int
countN n xs = length $ filter (== n) xs
countNumbers :: [Int] -> [Int]
countNumbers xs = map (\n -> countN n xs) [0..9]
repeatl :: Int -> x -> [x]
repeatl l w = take l $ repeat w
numbersToStars :: Int -> [Int] -> [String]
numbersToStars h (x:xs) = (repeatl x '*' ++ repeatl (h-x) ' ') : numbersToStars h xs
numbersToStars _ [] = []
*
*
*
*
* *
** **
*********
==========
0123456789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment