module Golf where -- Exercise 1 -- Explanation: -- takeNth takes the nth value of a list without crashing. First, it drops all -- the first `n - 1` elements from the list. Then, it takes the desired element. -- This function is total and can be thought of as the total version of the -- partial function `!!`. takeNth :: [a] -> Int -> [a] takeNth l n = take 1 $ drop (n-1) l -- Explanation: -- skips first maps all values in `nl`, defined as all values from 1 to the -- length of the list (excluded), to `concatMap`, which is the same as `concat -- (map ...)`. `concatMap` maps again the same array, this time multiplied by -- each value `n` coming from the outer map, to takeNth of the given list `l` -- and concatenates the result. skips :: [a] -> [[a]] skips [] = [] skips l = map (\n -> concatMap (takeNth l . (*n)) nl) nl where len = length l nl = [1..len]