summaryrefslogtreecommitdiff
path: root/lec07/Scrabble.hs
blob: 68dc55f430869996888c051c5c2ef713ba4f476f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Scrabble where

import Data.Char
import Data.List
import Data.Maybe

-- Exercise 3

newtype Score = Score Int
  deriving (Eq, Ord, Show, Num)

-- Needed due to changes in the standard library, see Sized.
instance Semigroup Score where
  (<>) = (+)

instance Monoid Score where
  mempty = Score 0

score :: Char -> Score
score c = Score $ fromMaybe 0 $ (!?) scores $ fromEnum (toUpper c) - fromEnum 'A'
  where scores = [1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10] :: [Int]

scoreString :: String -> Score
scoreString = foldl' (\acc c -> acc <> (score c)) mempty