From a48fdae020cad7e8ff6028ba8b4b6686ac5a22b8 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Sun, 14 Sep 2025 17:16:58 +0200 Subject: implement takeJ from second exercise of lecture 7 --- lec07/JoinList.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 -- cgit v1.2.3