hw7: exercise 3
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Thu, 25 Dec 2014 08:11:45 +0000 (13:41 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Thu, 25 Dec 2014 08:11:45 +0000 (13:41 +0530)
hw7/Scrabble.hs [new file with mode: 0644]

diff --git a/hw7/Scrabble.hs b/hw7/Scrabble.hs
new file mode 100644 (file)
index 0000000..b2dd38f
--- /dev/null
@@ -0,0 +1,30 @@
+{-# OPTIONS_GHC -Wall #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+
+module Scrabble where
+
+import Data.Monoid
+
+newtype Score = Score Int
+    deriving (Eq, Ord, Show, Num)
+
+getScore :: Score -> Int
+getScore (Score i) = i
+
+instance Monoid Score where
+  mempty = Score 0
+  mappend = (+)
+
+-- tile scoring rules as in http://www.thepixiepit.co.uk/scrabble/rules.html
+score :: Char -> Score
+score c | c `elem` "aeilnorstuAEILNORSTU" = 1
+        | c `elem` "dgDG" = 2
+        | c `elem` "bcmpBCMP" = 3
+        | c `elem` "fhvwyFHVWY" = 4
+        | c `elem` "kK" = 5
+        | c `elem` "jJxX" = 8
+        | c `elem` "qQzZ" = 10
+        | otherwise = 0
+
+scoreString :: String -> Score
+scoreString s = foldr mappend mempty (map score s)