Skip to content

Instantly share code, notes, and snippets.

@JulianBirch
Created May 22, 2014 21:33
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 JulianBirch/6444721b923f0646788d to your computer and use it in GitHub Desktop.
Save JulianBirch/6444721b923f0646788d to your computer and use it in GitHub Desktop.
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