ابتدای محتوای صفحه
۱۳ - ۱۳اضافه کردن یه newtype
یه راه دیگه که شاید بعضی جاها کُدمون رو خواناتر کنه، استفاده از newtype ِه:
-- با این تایپ مستعار جایگزین کنین
-- type WordList = [String]
newtyp WordList =
WordList [String]
deriving (Eq, Show)
allWords :: IO WordList
allWords = do
dict <- readFile "data/dict.txt"
return $ WordList (lines dict)
gameWords :: IO WordList
gameWords = do
(WordList aw) <- allWords
return $ WordList (filter gameLength aw)
where gameLength w =
let l = length (w :: String)
in l > minWordLength
&& l < maxWordLength
randomWord :: WordList -> IO String
randomWord (WordList wl) = do
randomIndex <-
randomRIO (0, (length wl) - 1)
return $ wl !! randomIndex