Last active
October 11, 2020 13:41
-
-
Save phagenlocher/c2d123ea2ec3a586661db3689049a0e4 to your computer and use it in GitHub Desktop.
This file contains 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
import Data.List | |
import Data.Char | |
import System.IO | |
getSearchWords :: IO [String] | |
getSearchWords = do | |
putStrLn "Specify the words to search:" | |
aux | |
where | |
aux = do | |
putStr "> " | |
line <- getLine | |
if line == "" then | |
return [] | |
else do | |
xs <- aux | |
return $ line:xs | |
lower :: String -> String | |
lower = map toLower | |
findStrings :: [String] -> String -> [String] | |
findStrings sws text = [ w | w <- sws, (lower w) `elem` txtwords ] | |
where | |
ftext = filter (\x -> isLetter x || isSpace x) text | |
txtwords = map lower $ words ftext | |
main :: IO () | |
main = do | |
hSetBuffering stdout NoBuffering | |
sws <- getSearchWords | |
putStr "File to search: " | |
path <- getLine | |
text <- readFile path | |
let found = findStrings sws text | |
let nfound = [ w | w <- sws, not $ w `elem` found ] | |
mapM_ (\s -> putStrLn $ "\"" ++ s ++ "\" found") found | |
mapM_ (\s -> putStrLn $ "\"" ++ s ++ "\" NOT found") nfound |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment