summaryrefslogtreecommitdiff
path: root/lec07/JoinList.hs
diff options
context:
space:
mode:
authorEdoardo La Greca2025-09-14 17:16:58 +0200
committerEdoardo La Greca2025-09-14 17:16:58 +0200
commita48fdae020cad7e8ff6028ba8b4b6686ac5a22b8 (patch)
treef670b93be5093583278a36d0c26f8a57112366ea /lec07/JoinList.hs
parentbaf67f76107a169e17bfe067fbc94e5afe175716 (diff)
implement takeJ from second exercise of lecture 7
Diffstat (limited to 'lec07/JoinList.hs')
-rw-r--r--lec07/JoinList.hs12
1 files changed, 12 insertions, 0 deletions
diff --git a/lec07/JoinList.hs b/lec07/JoinList.hs
index 5c1165e..ccfa838 100644
--- a/lec07/JoinList.hs
+++ b/lec07/JoinList.hs
@@ -43,3 +43,15 @@ dropJ n jl@(Append m jl1 jl2)
where
s1 = getSize $ size $ tag jl1
m' = getSize $ size m
+
+takeJ :: (Sized b, Monoid b) => Int -> JoinList b a -> JoinList b a
+takeJ _ Empty = Empty
+takeJ n _ | n <= 0 = Empty
+takeJ n jl@(Single _ _) = jl
+takeJ n jl@(Append m jl1 jl2)
+ | n >= m' = jl
+ | n < s1 = takeJ n jl1
+ | n >= s1 = jl1 +++ takeJ (n - s1) jl2
+ where
+ s1 = getSize $ size $ tag jl1
+ m' = getSize $ size m