Skip to content

Instantly share code, notes, and snippets.

@themattchan
Last active August 29, 2015 14:17
Show Gist options
  • Save themattchan/113cfc1b63dabbc95001 to your computer and use it in GitHub Desktop.
Save themattchan/113cfc1b63dabbc95001 to your computer and use it in GitHub Desktop.
module Golf where
import Data.List
skips :: [a] -> [[a]]
skips xs = map (map snd) $ zipWith filter filters (replicate len ids)
where len = length xs
ids = zip [1..len] xs
filters = map (\n (a,_) -> a `mod` n == 0) [1..len]
localMaxima :: [Integer] -> [Integer]
localMaxima xs = map (!!1) . filter maxs . take ((length xs) - 2) $ subs xs
where subs xs = xs : subs (tail xs)
maxs (x:y:z:_) = y > x && y > z
histogram :: [Integer] -> String
histogram = (++ fmt) . concatMap toStr . reverse . rows . assoc
where fmt = "==========\n0123456789\n"
assoc :: (Ord a) => [a] -> [(a,Int)]
assoc = map (\x -> (head x, length x)) . group . sort
rows xs = if all ((==0) . snd) xs then []
else xs : rows (map (\(n,c) -> (n, if c > 0 then c-1 else c)) xs)
toStr xs = (++"\n") $ concatMap f [0..9]
where f x = case lookup x xs of
Just 0 -> " "
Just _ -> "*"
Nothing -> " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment