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