Exercise 1 and 2: formableBy and wordsFrom
This commit is contained in:
parent
8eb0326df5
commit
fe0781b614
2 changed files with 61 additions and 0 deletions
22
Lecture 02/Scrabble/Scrabble.hs
Normal file
22
Lecture 02/Scrabble/Scrabble.hs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
module Scrabble where
|
||||||
|
|
||||||
|
import Words
|
||||||
|
import Data.List
|
||||||
|
|
||||||
|
type Hand = [Char]
|
||||||
|
|
||||||
|
formableBy :: String -> Hand -> Bool
|
||||||
|
formableBy [] _ = True
|
||||||
|
formableBy (x:xs) hand = let remainingHand = delete x hand in
|
||||||
|
elem x hand && formableBy xs remainingHand
|
||||||
|
|
||||||
|
wordsFrom :: Hand -> [String]
|
||||||
|
wordsFrom hand = wordsFromList hand allWords
|
||||||
|
|
||||||
|
wordsFromList :: Hand -> [String] -> [String]
|
||||||
|
wordsFromList hand [] = []
|
||||||
|
wordsFromList hand (x:[])
|
||||||
|
| formableBy x hand = [x]
|
||||||
|
| otherwise = []
|
||||||
|
wordsFromList hand (x:xs) = wordsFromList hand [x] ++ wordsFromList hand xs
|
||||||
|
|
39
Lecture 02/Scrabble/Words.hs
Normal file
39
Lecture 02/Scrabble/Words.hs
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue