summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 = []