diff options
| author | Edoardo La Greca | 2025-09-14 20:15:53 +0200 |
|---|---|---|
| committer | Edoardo La Greca | 2025-09-14 20:15:53 +0200 |
| commit | 52e7f3d06b22959212c114f557a392d986d65fc9 (patch) | |
| tree | 9402c3b2a85cb2d72af093d1846705c31a83413d | |
| parent | a48fdae020cad7e8ff6028ba8b4b6686ac5a22b8 (diff) | |
add third exercise of lecture 7
| -rw-r--r-- | lec07/JoinList.hs | 1 | ||||
| -rw-r--r-- | lec07/Scrabble.hs | 22 |
2 files changed, 23 insertions, 0 deletions
diff --git a/lec07/JoinList.hs b/lec07/JoinList.hs index ccfa838..5e1949f 100644 --- a/lec07/JoinList.hs +++ b/lec07/JoinList.hs @@ -1,6 +1,7 @@ module JoinList where import Sized +import Scrabble data JoinList m a = Empty | Single m a diff --git a/lec07/Scrabble.hs b/lec07/Scrabble.hs new file mode 100644 index 0000000..2c3b1f9 --- /dev/null +++ b/lec07/Scrabble.hs @@ -0,0 +1,22 @@ +module Scrabble where +import Data.Char +import Data.List ((!?)) + +-- 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' <> mempty |