From: Ramakrishnan Muthukrishnan Date: Thu, 25 Dec 2014 02:59:00 +0000 (+0530) Subject: hw7: revisit indexJ X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/flags/simplejson/frontends/nxhtml.html?a=commitdiff_plain;h=94a695319fabb1dd0ad35532dadc3cf68840c856;p=yorgey.git hw7: revisit indexJ --- diff --git a/hw7/JoinList.hs b/hw7/JoinList.hs index 7f13837..3dca2de 100644 --- a/hw7/JoinList.hs +++ b/hw7/JoinList.hs @@ -26,19 +26,10 @@ indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a indexJ _ Empty = Nothing indexJ n _ | n < 0 = Nothing -indexJ _ (Single _ a) = Just a -indexJ n (Append _ l r) | n < (getSize (size (tag l))) = indexJ n l +indexJ n (Single _ a) | n == 0 = Just a + | otherwise = Nothing +indexJ n (Append t l r) | n >= getSize (size t) = Nothing + | n < 0 = Nothing + | n < (getSize (size (tag l))) = indexJ n l | otherwise = indexJ (n - getSize (size (tag l))) r --- 2. drop -dropJ :: (Sized b, Monoid b) => - Int -> JoinList b a -> JoinList b a -dropJ n x | n < 0 = x -dropJ 0 x = x -dropJ _ Empty = Empty -dropJ _ (Single _ _) = Empty -dropJ n x | (getSize (size (tag x))) < n = Empty -dropJ n (Append _ l r) | n > (getSize (size (tag l))) = - dropJ (n - getSize (size (tag l))) r - | otherwise = let lft = dropJ n l in - Append ((tag lft) `mappend` (tag r)) lft r