From 5ecf35a1ec67727c695505b1c14e8ea7794e3117 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Thu, 25 Dec 2014 15:18:40 +0530 Subject: [PATCH] initial version of the assignment solution. WIP --- hw7/JoinList.hs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ hw7/Scrabble.hs | 1 - 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/hw7/JoinList.hs b/hw7/JoinList.hs index 916597e..e5633d5 100644 --- a/hw7/JoinList.hs +++ b/hw7/JoinList.hs @@ -1,8 +1,14 @@ {-# 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 @@ -62,3 +68,46 @@ takeJ n v@(Append t l r) | n > getSize (size t) = v 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 (Empty) = "" + toString (Single _ s) = s + toString (Append _ l r) = toString l ++ toString r + + fromString s = Single (scoreString s, (Size (length s))) s + 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." diff --git a/hw7/Scrabble.hs b/hw7/Scrabble.hs index b2dd38f..b08f6af 100644 --- a/hw7/Scrabble.hs +++ b/hw7/Scrabble.hs @@ -1,6 +1,5 @@ {-# OPTIONS_GHC -Wall #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} - module Scrabble where import Data.Monoid -- 2.37.2