Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code Golf
{-# OPTIONS_GHC -Wall #-}
module Golf where
import qualified Data.Map.Strict as M
import Data.List as L
import Data.Maybe
skip :: [a] -> Int -> [a]
skip l n = case drop n l of
(x:xs) -> x : skip xs n
_ -> []
skips :: [a] -> [[a]]
skips l = map (skip l) $ take (length l) [0..]
isLocalMaximum :: Ord a => [a] -> Bool
isLocalMaximum (a:b:c:_) = b > a && b > c
isLocalMaximum _ = False
second :: [a] -> a
second (_:b:_) = b
second _ = undefined
partitions :: Int -> [a] -> [[a]]
partitions n l@(_:xs) = let p = take n l
in if (length p == n)
then p:partitions n xs
else []
partitions _ _ = []
localMaxima :: [Integer] -> [Integer]
localMaxima = map second . filter isLocalMaximum . partitions 3
histogram :: [Integer] -> String
histogram i = let b = [0..9]
r0 = L.group $ L.sort i
v = map length r0
r = M.fromList $ zip (map head r0) v
-- Given two functions f and g,
-- how do I map to a list of (f x, g x)
-- pairs elegantly?
h = foldr max 0 v
star x = if x then ' ' else '*'
m n = star . (< n)
. fromMaybe 0
. flip M.lookup r
line n = map (m n) b
s _ = '='
alllines = reverse $
(concatMap show b) :(map s b) : map line [1..h]
in concatMap (++ "\n") alllines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.