summaryrefslogtreecommitdiff
path: root/lec07
diff options
context:
space:
mode:
authorEdoardo La Greca2025-09-10 20:46:40 +0200
committerEdoardo La Greca2025-09-10 21:16:52 +0200
commit125dcc2372f01da088b603676bf4870e6aafb843 (patch)
tree9ce27daeec5b051a16b572203b1d51100e066f01 /lec07
parent2c3d5c3504f38c57c0e4abc89da91fcec015151e (diff)
implement indexJ from second exercise of lecture 7
Diffstat (limited to 'lec07')
-rw-r--r--lec07/JoinList.hs13
1 files changed, 13 insertions, 0 deletions
diff --git a/lec07/JoinList.hs b/lec07/JoinList.hs
index d04a0c6..7ec30a9 100644
--- a/lec07/JoinList.hs
+++ b/lec07/JoinList.hs
@@ -15,3 +15,16 @@ tag Append m _ _ = m
(+++) :: Monoid m => JoinList m a -> JoinList m a -> JoinList m a
(+++) jl1 jl2 = mappend jl1 jl2
+-- Exercise 2
+
+indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a
+indexJ _ Empty = Nothing
+indexJ n (Single _ a)
+ | n == 0 = Just a
+ | otherwise = Nothing
+indexJ n (Append m jl1 jl2)
+ | n < 0 || n >= m = Nothing
+ | n < tag1 = indexJ n jl1
+ | n >= tag1 = indexJ (n - tag1) jl2
+ where tag1 = tag jl1
+