Haskell将外部txt文件加载到列表中

tut*_*utu 6 haskell

您好以下代码是一个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]
Run Code Online (Sandbox Code Playgroud)

Aar*_*oup 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)
Run Code Online (Sandbox Code Playgroud)

此外,您可能必须阅读Haskell中的IO(我建议使用Google搜索'io haskell教程'),如果您还没有这样做的话.您还需要它来为您的程序引入交互性.