From: Ramakrishnan Muthukrishnan Date: Thu, 25 Dec 2014 03:18:36 +0000 (+0530) Subject: hw7: add takeJ X-Git-Url: https://git.rkrishnan.org/?p=yorgey.git;a=commitdiff_plain;h=1212cb83071a0f74d0202bb5bffcf5e348cb7a91 hw7: add takeJ --- 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 +