Implemented exercise 3: wordFitsTemplate
This commit is contained in:
parent
7bdeaff608
commit
a4206d103d
@ -10,9 +10,6 @@ formableBy [] _ = True
|
|||||||
formableBy (x:xs) hand = let remainingHand = delete x hand in
|
formableBy (x:xs) hand = let remainingHand = delete x hand in
|
||||||
elem x hand && formableBy xs remainingHand
|
elem x hand && formableBy xs remainingHand
|
||||||
|
|
||||||
wordsFrom :: Hand -> [String]
|
|
||||||
wordsFrom hand = wordsFromList hand allWords
|
|
||||||
|
|
||||||
wordsFromList :: Hand -> [String] -> [String]
|
wordsFromList :: Hand -> [String] -> [String]
|
||||||
wordsFromList hand [] = []
|
wordsFromList hand [] = []
|
||||||
wordsFromList hand (x:[])
|
wordsFromList hand (x:[])
|
||||||
@ -20,3 +17,26 @@ wordsFromList hand (x:[])
|
|||||||
| otherwise = []
|
| otherwise = []
|
||||||
wordsFromList hand (x:xs) = wordsFromList hand [x] ++ wordsFromList hand xs
|
wordsFromList hand (x:xs) = wordsFromList hand [x] ++ wordsFromList hand xs
|
||||||
|
|
||||||
|
wordsFrom :: Hand -> [String]
|
||||||
|
wordsFrom hand = wordsFromList hand allWords
|
||||||
|
|
||||||
|
type Template = String
|
||||||
|
|
||||||
|
wordMatchesTemplate :: String -> Template -> Bool
|
||||||
|
wordMatchesTemplate [] [] = True
|
||||||
|
wordMatchesTemplate (w:ws) (t:ts)
|
||||||
|
| t == '?' = wordMatchesTemplate ws ts
|
||||||
|
| otherwise = w == t && wordMatchesTemplate ws ts
|
||||||
|
wordMatchesTemplate _ _ = False
|
||||||
|
|
||||||
|
removeTemplateChars :: String -> Template -> String
|
||||||
|
removeTemplateChars word [] = word
|
||||||
|
removeTemplateChars word (t:ts)
|
||||||
|
| t == '?' = removeTemplateChars word ts
|
||||||
|
| otherwise = removeTemplateChars (delete t word) ts
|
||||||
|
|
||||||
|
wordFitsTemplate :: Template -> Hand -> String -> Bool
|
||||||
|
wordFitsTemplate template hand word = let matchWord = removeTemplateChars word template in
|
||||||
|
formableBy matchWord hand && wordMatchesTemplate word template
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user