1 {-# OPTIONS_GHC -Wall #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE TypeSynonymInstances #-}
13 data JoinList m a = Empty
15 | Append m (JoinList m a) (JoinList m a)
19 tag :: Monoid m => JoinList m a -> m
22 tag (Append t _ _) = t
24 (+++) :: Monoid m => JoinList m a -> JoinList m a -> JoinList m a
27 alst1 +++ alst2 = Append ((tag alst1) `mappend` (tag alst2)) alst1 alst2
31 indexJ :: (Sized b, Monoid b) =>
32 Int -> JoinList b a -> Maybe a
33 indexJ _ Empty = Nothing
34 indexJ n _ | n < 0 = Nothing
35 indexJ n (Single _ a) | n == 0 = Just a
37 indexJ n (Append t l r) | n >= getSize (size t) = Nothing
39 | n < (getSize (size (tag l))) = indexJ n l
40 | otherwise = indexJ (n - getSize (size (tag l))) r
43 dropJ :: (Sized b, Monoid b) =>
44 Int -> JoinList b a -> JoinList b a
48 dropJ n v@(Single _ _) | n == 0 = v
50 dropJ n (Append t l r) | n > getSize (size t) = Empty
52 dropJ (n - leftSize) r
53 | otherwise = let lft = dropJ n l in
54 Append ((tag lft) `mappend` (tag r)) lft r
55 where leftSize = getSize . size . tag $ l
58 takeJ :: (Sized b, Monoid b) =>
59 Int -> JoinList b a -> JoinList b a
61 takeJ n _ | n < 0 = Empty
63 takeJ n v@(Single _ _) | n == 0 = v
65 takeJ n v@(Append t l r) | n > getSize (size t) = v
66 | n < leftSize = takeJ n l
67 | otherwise = let rt = takeJ (n - leftSize) r in
68 Append ((tag l) `mappend` (tag rt)) l rt
69 where leftSize = getSize . size . tag $ l
72 -- exercise 3 (Scrabble.hs)
73 scoreLine :: String -> JoinList Score String
74 scoreLine l = let scores = map scoreString . words $ l
75 scoresAndWords = zip scores (words l)
77 foldr (+++) Empty $ map (\(s, w) -> Single s w) scoresAndWords
80 -- instance (Monoid a, Monoid b) => Monoid (a,b) where
81 -- mempty = (mempty, mempty)
82 -- (a,b) `mappend` (c,d) = ((a `mappend` c), (b `mappend` d))
85 instance Buffer (JoinList (Score, Size) String) where
87 toString (Single _ s) = s
88 toString (Append _ l r) = toString l ++ toString r
90 fromString s = Single (scoreString s, (Size (length s))) s
92 replaceLine n l b = let pre = takeJ (n - 1) b
94 (Append m1 _ _) = scoreLine l
96 pre +++ (Single (m1, Size (length l)) l) +++ post
97 numLines = \x -> case x of
100 Append _ l r -> numLines l + numLines r
102 value = \x -> case x of
104 Single (_, m2) _ -> getSize . size $ m2
105 Append (_, m2) _ _ -> getSize . size $ m2
107 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."
108 -- [ "This buffer is for notes you don't want to save, and for"
109 -- , "evaluation of steam valve coefficients."
110 -- , "To load a different file, type the character L followed"
111 -- , "by the name of the file."
113 -- "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."