From a02a185d40ff6c141a2daa291352a4d6e0243772 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Thu, 25 Dec 2014 13:41:45 +0530 Subject: [PATCH] hw7: exercise 3 --- hw7/Scrabble.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 hw7/Scrabble.hs diff --git a/hw7/Scrabble.hs b/hw7/Scrabble.hs new file mode 100644 index 0000000..b2dd38f --- /dev/null +++ b/hw7/Scrabble.hs @@ -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) -- 2.45.2