-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrabble.hs
56 lines (48 loc) · 840 Bytes
/
Scrabble.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Scrabble
(score,
scoreString,
Score,
getScore)
where
import Data.Monoid
import qualified Data.Map as Map
newtype Score = Score { getScore :: Int }
deriving (Eq, Show, Ord, Num)
instance Monoid Score where
mempty = Score 0
mappend = (+)
scoreMap :: Map.Map Char Int
scoreMap = Map.fromList [
('a', 1),
('b', 3),
('c', 3),
('d', 2),
('e', 1),
('f', 4),
('g', 2),
('h', 4),
('i', 1),
('j', 8),
('k', 5),
('l', 1),
('m', 3),
('n', 1),
('o', 1),
('p', 3),
('q', 10),
('r', 1),
('s', 1),
('t', 1),
('u', 1),
('v', 4),
('w', 4),
('x', 8),
('y', 4),
('z', 10)]
score :: Char -> Score
score c = case Map.lookup c scoreMap of
Just s -> Score s
Nothing -> Score 0
scoreString :: String -> Score
scoreString = sum . map score