]> git.rkrishnan.org Git - yorgey.git/blob - hw7/Scrabble.hs
initial version of the assignment solution. WIP
[yorgey.git] / hw7 / Scrabble.hs
1 {-# OPTIONS_GHC -Wall #-}
2 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
3 module Scrabble where
4
5 import Data.Monoid
6
7 newtype Score = Score Int
8     deriving (Eq, Ord, Show, Num)
9
10 getScore :: Score -> Int
11 getScore (Score i) = i
12
13 instance Monoid Score where
14   mempty = Score 0
15   mappend = (+)
16
17 -- tile scoring rules as in http://www.thepixiepit.co.uk/scrabble/rules.html
18 score :: Char -> Score
19 score c | c `elem` "aeilnorstuAEILNORSTU" = 1
20         | c `elem` "dgDG" = 2
21         | c `elem` "bcmpBCMP" = 3
22         | c `elem` "fhvwyFHVWY" = 4
23         | c `elem` "kK" = 5
24         | c `elem` "jJxX" = 8
25         | c `elem` "qQzZ" = 10
26         | otherwise = 0
27
28 scoreString :: String -> Score
29 scoreString s = foldr mappend mempty (map score s)