您好以下代码是一个wordfeud程序。它允许您搜索与前缀,后缀和一些字母匹配的单词列表。我的问题是,我不想使用底部的列表,而是使用包含单词的外部文本文件并将其加载到列表中。我该怎么做呢?
count :: String -> String -> Int
count _[] = 0
count [] _ = 0
count (x:xs) square
|x `elem` square = 1 + count xs (delete x square)
|otherwise = count xs square
check :: String -> String -> String -> String -> Bool
check prefix suffix word square
| (length strippedWord) == (count strippedWord square) = True
| otherwise = False
where
strippedWord = drop (length prefix) (take ((length word ) - (length suffix)) word)
wordfeud :: String -> String -> String -> [String]
wordfeud a b c = test1
where
test =["horse","chair","chairman","bag","house","mouse","dirt","sport"]
test1 = [x| x <- test, a `isPrefixOf` x, b `isSuffixOf` x, check a b x c]
答案 0 :(得分:5)
非常简单,在lines
函数(或words
的帮助下,当单词被其他形式的空格分隔而不是换行时:
-- Loads words from a text file into a list.
getWords :: FilePath -> IO [String]
getWords path = do contents <- readFile path
return (lines contents)
此外,你可能必须阅读Haskell中的IO(如果你还没有这样做的话,我推荐谷歌搜索'io haskell教程')。您还需要它来为您的程序引入交互性。