diff options
| author | Edoardo La Greca | 2025-09-14 17:16:58 +0200 |
|---|---|---|
| committer | Edoardo La Greca | 2025-09-14 17:16:58 +0200 |
| commit | a48fdae020cad7e8ff6028ba8b4b6686ac5a22b8 (patch) | |
| tree | f670b93be5093583278a36d0c26f8a57112366ea | |
| parent | baf67f76107a169e17bfe067fbc94e5afe175716 (diff) | |
implement takeJ from second exercise of lecture 7
| -rw-r--r-- | lec07/JoinList.hs | 12 |
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 |