From: Ramakrishnan Muthukrishnan Date: Thu, 25 Dec 2014 08:11:45 +0000 (+0530) Subject: hw7: exercise 3 X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/readonly?a=commitdiff_plain;h=a02a185d40ff6c141a2daa291352a4d6e0243772;p=yorgey.git hw7: exercise 3 --- 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)