From 1212cb83071a0f74d0202bb5bffcf5e348cb7a91 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org> Date: Thu, 25 Dec 2014 08:48:36 +0530 Subject: [PATCH] hw7: add takeJ --- hw7/JoinList.hs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 + -- 2.45.2