diff options
author | Edoardo La Greca | 2025-07-02 20:04:26 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-07-02 20:04:26 +0200 |
commit | 08ef4a40c204410270b1fc907c1cd3727085e707 (patch) | |
tree | 1c503beab40232567bcf081edb50cd0a0b4eac89 /lec03/Golf.hs | |
parent | 19175762c6025ec21ef836cc081711f8f9897034 (diff) |
add first exercise of lecture 3
Diffstat (limited to 'lec03/Golf.hs')
-rw-r--r-- | lec03/Golf.hs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lec03/Golf.hs b/lec03/Golf.hs new file mode 100644 index 0000000..eb69136 --- /dev/null +++ b/lec03/Golf.hs @@ -0,0 +1,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] |