summaryrefslogtreecommitdiff
path: root/lec03
diff options
context:
space:
mode:
authorEdoardo La Greca2025-07-03 21:22:51 +0200
committerEdoardo La Greca2025-07-03 21:22:51 +0200
commit27ae61a5ee94e413d2f9c1aefb1406454a3ac7d1 (patch)
tree46f64e15363b6502873cfaf7bad9f0993df1243a /lec03
parent23a4d27e4f331abc31bee3e2bc418fc9ceb3ec21 (diff)
add second exercise of lecture 3
Diffstat (limited to 'lec03')
-rw-r--r--lec03/Golf.hs17
1 files changed, 17 insertions, 0 deletions
diff --git a/lec03/Golf.hs b/lec03/Golf.hs
index b89aa57..e4fa9a3 100644
--- a/lec03/Golf.hs
+++ b/lec03/Golf.hs
@@ -22,3 +22,20 @@ skips l = map (\n -> concatMap (takeNth l . (*n)) nl) nl
where
len = length l
nl = [1..len]
+
+-- Exercise 2
+
+-- Explanation:
+-- localMaxima is defined in two cases. In the first case, the argument of the
+-- function is a list with at least 3 elments. Depending on the value of the
+-- first three elements, checked through two guards, the second is either
+-- included or excluded from the resulting list. In particular, the first guard
+-- checks whether both the first and the third are smaller than the second using
+-- the `all` function. The second case matches if the list has less than 3
+-- elements, in which case the result is just an empty list.
+-- The list resulting from the `localMaxima` function is built recursively.
+localMaxima :: [Integer] -> [Integer]
+localMaxima (x1:x2:x3:xs)
+ | all (<x2) [x1,x3] = x2 : localMaxima (x2:x3:xs)
+ | otherwise = localMaxima (x2:x3:xs)
+localMaxima l = []