X-Git-Url: https://git.rkrishnan.org/?a=blobdiff_plain;f=hw7%2FJoinList.hs;h=7a353cded5f694c8e7fc606a1258bc8d8d86142a;hb=1212cb83071a0f74d0202bb5bffcf5e348cb7a91;hp=e498eedeee67aca1d79710a856558bce0918e7bb;hpb=4e8d48f0e21062d9aeb7ab96fc6478560a421812;p=yorgey.git diff --git a/hw7/JoinList.hs b/hw7/JoinList.hs index e498eed..7a353cd 100644 --- a/hw7/JoinList.hs +++ b/hw7/JoinList.hs @@ -47,3 +47,16 @@ dropJ n (Append t l r) | n > getSize (size t) = Empty | otherwise = let lft = dropJ n l in Append ((tag lft) `mappend` (tag r)) lft r +-- 3. take +takeJ :: (Sized b, Monoid b) => + Int -> JoinList b a -> JoinList b a +takeJ 0 _ = Empty +takeJ n _ | n < 0 = Empty +takeJ _ Empty = Empty +takeJ n v@(Single _ _) | n == 0 = v + | otherwise = Empty +takeJ n v@(Append t l r) | n > getSize (size t) = v + | n < getSize (size (tag l)) = takeJ n l + | otherwise = let rt = takeJ (n - getSize (size (tag l))) r in + Append ((tag l) `mappend` (tag rt)) l rt +