]> git.rkrishnan.org Git - yorgey.git/blobdiff - hw7/JoinList.hs
bug fix. Convert into lines
[yorgey.git] / hw7 / JoinList.hs
index b7a4aeb949599db702dfd0696d9e4cd2a4261581..84c3a41b268295200a7e22e24c1d7d70e79b5b60 100644 (file)
@@ -1,14 +1,25 @@
 {-# OPTIONS_GHC -Wall #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE TypeSynonymInstances #-}
+
 module JoinList where
 
 import Data.Monoid
 import Sized
+import Scrabble
+import Buffer
+import Editor
 
 data JoinList m a = Empty
                   | Single m a
                   | Append m (JoinList m a) (JoinList m a)
                     deriving (Eq, Show)
 
+jlToList :: JoinList m a -> [a]
+jlToList Empty = []
+jlToList (Single m a) = [a]
+jlToList (Append m lt rt) = jlToList lt ++ jlToList rt
+
 -- exercise 1
 tag :: Monoid m => JoinList m a -> m
 tag (Empty) = mempty
@@ -26,18 +37,81 @@ 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
+dropJ n v@(Single _ _) | n == 0 = v
+                       | otherwise = Empty
+dropJ n (Append t l r) | n > getSize (size t) = Empty
+                       | n > leftSize =
+                           dropJ (n - leftSize) r
                        | otherwise = let lft = dropJ n l in
                                      Append ((tag lft) `mappend` (tag r)) lft r
+                       where leftSize = getSize . size . tag $ l
+
+-- 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 < leftSize = takeJ n l
+                         | otherwise = let rt = takeJ (n - leftSize) r in
+                                       Append ((tag l) `mappend` (tag rt)) l rt
+                         where leftSize = getSize . size . tag $ l
+
+
+-- exercise 3 (Scrabble.hs)
+scoreLine :: String -> JoinList Score String
+scoreLine l = let scores = map scoreString . words $ l
+                  scoresAndWords = zip scores (words l)
+              in
+                foldr (+++) Empty $ map (\(s, w) -> Single s w) scoresAndWords
+
+-- exercise 4
+-- instance (Monoid a, Monoid b) => Monoid (a,b) where
+--   mempty = (mempty, mempty)
+--   (a,b) `mappend` (c,d) = ((a `mappend` c), (b `mappend` d))
+
+
+instance Buffer (JoinList (Score, Size) String) where
+  toString = unlines . jlToList
+
+  fromString s = let ls = lines s in
+                  foldr (+++) Empty $ map (\x -> Single (scoreString x, Size 1) x) ls
+  line n b = indexJ n b
+  replaceLine n l b = let pre = takeJ (n - 1) b
+                          post = dropJ n b
+                          (Append m1 _ _) = scoreLine l
+                      in
+                       pre +++ (Single (m1, Size (length l)) l) +++ post
+  numLines = \x -> case x of
+                     Empty -> 0
+                     Single _ _ -> 1
+                     Append _ l r -> numLines l + numLines r
+
+  value = \x -> case x of
+                  Empty -> 0
+                  Single (_, m2) _ -> getSize . size $ m2
+                  Append (_, m2) _ _ -> getSize . size $ m2
+
+main = runEditor editor $ Single (Score 244,Size 177) "This buffer is for notes you don't want to save, and for evaluation of steam valve coefficients. To load a different file, type the character L followed by the name of the file."
+--         [ "This buffer is for notes you don't want to save, and for"
+--         , "evaluation of steam valve coefficients."
+--         , "To load a different file, type the character L followed"
+--         , "by the name of the file."
+--         ]
+-- "This buffer is for notes you don't want to save, and for evaluation of steam valve -- coefficients. To load a different file, type the character L followed by the name of the file."