From 45444928a772dd1542f16a2813423c1b9562724e Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Wed, 24 Dec 2014 22:13:41 +0530
Subject: [PATCH] hw7: exercise 2, part 1

---
 hw7/JoinList.hs | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

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
+
-- 
2.45.2