module Golf where

skips :: [a] -> [[a]]
skips ls = map (\i -> everyN i ls) [1..length ls]

everyN :: Int -> [a] -> [a]
everyN n [] = []
everyN n ls = loop n (drop (n-1) ls)
  where
    loop n [] = []
    loop n (x:xs) = x : loop n (drop (n-1) xs)

localMaxima :: [Int] -> [Int]
localMaxima (x:y:z:zs)
  | x < y && y > z = y : localMaxima (y:z:zs)
  | otherwise = localMaxima (y:z:zs)
localMaxima zs = []

-- putStrLn $ histogram [1,2,1]
histogram :: [Int] -> String
histogram xs = drawLines (reverse $ makeH $ group xs) ++ "==========\n0123456789\n"

group :: [Int] -> [[Int]]
group xs = map (\n -> filter (==n) xs) [0..9]

makeH :: [[Int]] -> [[[Int]]]
makeH [[],[],[],[],[],[],[],[],[],[]] = []
makeH xss = (heads xss) : makeH (tails xss)

heads :: [[Int]] -> [[Int]]
heads xss = map (take 1) xss

tails :: [[Int]] -> [[Int]]
tails xss = map (drop 1) xss

drawLines :: [[[Int]]] -> String
drawLines xsss = concat $ map drawLine xsss

drawLine :: [[Int]] -> String
drawLine xss = (map draw xss) ++ "\n"

draw :: [Int] -> Char
draw [] = ' '
draw _ = '*'

