Last active
February 22, 2019 09:40
-
-
Save kubaracek/71c3e084eb4271ee0114cb26ccdeb141 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | | |
module Game where | |
import Data.List (isInfixOf, sort, nub, any) | |
newtype Available = Available String deriving (Show, Eq) | |
type Word = [Char] | |
data WordError = NothingAvailable | CharNotAvailable Char deriving (Show) | |
deleteFirst _ [] = [] | |
deleteFirst a (b:bc) | |
| a == b = bc | |
| otherwise = b : deleteFirst a bc | |
useWord :: Game.Word -> Available -> Either WordError Available | |
useWord [] (Available "") = Right $ Available "" | |
useWord (_) (Available "") = Left NothingAvailable | |
useWord [] (Available a) = Right $ Available a | |
useWord (x:xs) (Available y) | |
| elem x y = useWord xs $ Available $ deleteFirst x y | |
| otherwise = Left $ CharNotAvailable x | |
guardWord :: Available -> Game.Word -> Either WordError Bool | |
guardWord available word = | |
fmap null $ useWord word available |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment