From: Ramakrishnan Muthukrishnan Date: Wed, 24 Dec 2014 16:43:41 +0000 (+0530) Subject: hw7: exercise 2, part 1 X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/htmlfontify-example.html?a=commitdiff_plain;h=45444928a772dd1542f16a2813423c1b9562724e;p=yorgey.git hw7: exercise 2, part 1 --- diff --git a/hw7/JoinList.hs b/hw7/JoinList.hs index 4edc744..6ef51a8 100644 --- a/hw7/JoinList.hs +++ b/hw7/JoinList.hs @@ -2,7 +2,7 @@ module JoinList where import Data.Monoid - +import Sized data JoinList m a = Empty | Single m a @@ -20,3 +20,13 @@ Empty +++ x = x x +++ Empty = x alst1 +++ alst2 = Append ((tag alst1) `mappend` (tag alst2)) alst1 alst2 +-- exercise 2 +-- 1. index +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 + | otherwise = indexJ (n - getSize (size (tag l))) r +