Haskell uploads external txt file to list

Hello, the following code is the wordfeud program. This allows you to search for a list of words matching the prefix, suffix, and some letters. My question is that instead of using the list below, I want to use an external text file containing words and load it into the list. How can I do it?

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 abc = test1 where test =["horse","chair","chairman","bag","house","mouse","dirt","sport"] test1 = [x| x <- test, a `isPrefixOf` x, b `isSuffixOf` x, check abxc] 
+6
source share
1 answer

Very simple, using the lines function (or words , when words are separated by some other form of spaces than line breaks):

 -- Loads words from a text file into a list. getWords :: FilePath -> IO [String] getWords path = do contents <- readFile path return (lines contents) 

Also, you probably have to read the IO in Haskell (I recommend the googling 'io haskell tutorial') if you haven't already. You will also need to introduce interactivity into your program.

+5
source

Source: https://habr.com/ru/post/919399/


All Articles