summaryrefslogtreecommitdiff
path: root/lec07/JoinList.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lec07/JoinList.hs')
-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
+