From 08ef4a40c204410270b1fc907c1cd3727085e707 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Wed, 2 Jul 2025 20:04:26 +0200 Subject: add first exercise of lecture 3 --- lec03/Golf.hs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lec03/Golf.hs 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] -- cgit v1.2.3