Created
September 16, 2014 05:36
-
-
Save paul-english/2ca31bda9244a56e8ab6 to your computer and use it in GitHub Desktop.
haskell-hw02.hs
This file contains hidden or 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
| -- Exercise 1 | |
| formableBy :: String -> Hand -> Bool | |
| formableBy [] _ = True | |
| formableBy [letter] hand = letter `elem` hand | |
| formableBy (letter:letters) hand | |
| | letter `elem` hand = formableBy letters (delete letter hand) | |
| | otherwise = False | |
| -- Exercise 2 | |
| checkWords :: [String] -> [String] -> Hand -> [String] | |
| checkWords formableWords [] _ = formableWords | |
| checkWords formableWords [word] hand | |
| | formableBy word hand = (word:formableWords) | |
| | otherwise = formableWords | |
| checkWords formableWords (word:words) hand | |
| | formableBy word hand = checkWords (word:formableWords) words hand | |
| | otherwise = checkWords formableWords words hand | |
| wordsFrom :: Hand -> [String] | |
| wordsFrom [] = [] | |
| wordsFrom hand = reverse (checkWords [] (allWords) hand) | |
| -- Exercise 3 | |
| wordFitsTemplate :: Template -> Hand -> String -> Bool | |
| wordFitsTemplate [] _ [] = True | |
| wordFitsTemplate [t] _ [] = False | |
| wordFitsTemplate template _ [] = False | |
| wordFitsTemplate [] _ [w] = False | |
| wordFitsTemplate [] _ word = False | |
| wordFitsTemplate (t:template) hand (w:word) | |
| | (t == '?') && (formableBy [w] hand) = wordFitsTemplate template hand word | |
| | t == w = wordFitsTemplate template hand word | |
| | otherwise = False | |
| -- Exercise 4 | |
| stripQ :: [Char] -> [Char] | |
| stripQ [] = [] | |
| stripQ ('?':a) = stripQ a | |
| stripQ (a:b) = a : stripQ b | |
| checkTemplates :: [String] -> Hand -> Template -> [String] -> [String] | |
| checkTemplates acc _ _ [] = acc | |
| checkTemplates acc hand template (word:words) | |
| | wordFitsTemplate template hand word = checkTemplates (word:acc) hand template words | |
| | otherwise = checkTemplates acc hand template words | |
| wordsFittingTemplate :: Template -> Hand -> [String] | |
| wordsFittingTemplate _ [] = [] | |
| wordsFittingTemplate template hand = reverse (checkTemplates [] hand template allWords) | |
| where | |
| allWords = (wordsFrom (hand ++ (stripQ template))) | |
| -- Exercise 5 | |
| scrabbleValueWord :: String -> Int | |
| scrabbleValueWord [] = 0 | |
| scrabbleValueWord (x:xs) = (scrabbleValue x) + scrabbleValueWord xs | |
| -- Exercise 6 | |
| fetchHighestScoring :: [String] -> Int -> [String] -> [String] | |
| fetchHighestScoring highest _ [] = highest | |
| fetchHighestScoring highest max (w:words) | |
| | value > max = fetchHighestScoring [w] value words | |
| | value == max = fetchHighestScoring (w:highest) max words | |
| | otherwise = fetchHighestScoring highest max words | |
| where | |
| value = scrabbleValueWord w | |
| bestWords :: [String] -> [String] | |
| bestWords [] = [] | |
| bestWords words = fetchHighestScoring [] 0 words | |
| -- Exercise 7 | |
| replaceScrabbleValChars :: [Char] -> [Char] | |
| replaceScrabbleValChars [] = [] | |
| replaceScrabbleValChars ('D':a) = '?' : replaceScrabbleValChars a | |
| replaceScrabbleValChars ('T':a) = '?' : replaceScrabbleValChars a | |
| replaceScrabbleValChars ('2':a) = '?' : replaceScrabbleValChars a | |
| replaceScrabbleValChars ('3':a) = '?' : replaceScrabbleValChars a | |
| replaceScrabbleValChars (a:b) = a : replaceScrabbleValChars b | |
| scrabbleValueTemplateScore :: Int -> Int -> STemplate -> String -> Int | |
| scrabbleValueTemplateScore score multiplier [] [] = score * multiplier | |
| scrabbleValueTemplateScore score multiplier (t:template) (w:word) | |
| | t == 'D' = scrabbleValueTemplateScore (score + ((scrabbleValueWord [w]) * 2)) multiplier template word | |
| | t == 'T' = scrabbleValueTemplateScore (score + ((scrabbleValueWord [w]) * 3)) multiplier template word | |
| | t == '2' = scrabbleValueTemplateScore (score + (scrabbleValueWord [w])) (multiplier * 2) template word | |
| | t == '3' = scrabbleValueTemplateScore (score + (scrabbleValueWord [w])) (multiplier * 3) template word | |
| | otherwise = scrabbleValueTemplateScore (score + (scrabbleValueWord [w])) multiplier template word | |
| scrabbleValueTemplate :: STemplate -> String -> Int | |
| scrabbleValueTemplate template word = scrabbleValueTemplateScore 0 1 template word |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment