blob: eb691369e708a3cbed6e9a6057a93bf87fc6d3a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
module Golf where
-- Exercise 1
-- Explanation:
-- takeEvery takes all elements with index equal or multiple of a given
-- positive number, index 0 exluded. In the first two cases, it checks whether
-- the list is empty or the number is zero, and returns an empty list if so. In
-- the next case, which is the most general, it defines l2 and l3, which are
-- respectively the list without the first n-1 and n elements. Then it takes
-- the fist element out of l2, which, relative to the list, has an index
-- multiple of (or equal to) the given number, and then recursively call the
-- function itself on l3 so that it drops another (n-1) elements and takes the
-- one whose index is multiple of the number, etc.
takeEvery :: [a] -> Int -> [a]
takeEvery [] _ = []
takeEvery _ 0 = []
takeEvery l n = take 1 l2 ++ takeEvery l3 n
where
l2 = drop (n-1) l
l3 = drop 1 l2
-- Explanation:
-- skips calls takeEvery for every number from 1 to the length of the list - 1.
skips :: [a] -> [[a]]
skips [] = []
skips l = map (takeEvery l) [1..length l]
|