Skip to content

Instantly share code, notes, and snippets.

@kubaracek
Last active February 22, 2019 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubaracek/71c3e084eb4271ee0114cb26ccdeb141 to your computer and use it in GitHub Desktop.
Save kubaracek/71c3e084eb4271ee0114cb26ccdeb141 to your computer and use it in GitHub Desktop.
-- |
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