제목에서 알 수 있듯이 주어진 경로에서 주어진 문자열을 찾으려고합니다. 여기에 지금까지 올 것입니다 :주어진 경로에서 주어진 문자열 찾기
getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents topdir = do
names <- getDirectoryContents topdir
let properNames = filter (`notElem` [".", ".."]) names
paths <- forM properNames $ \name -> do
let path = topdir </> name
isDirectory <- doesDirectoryExist path
if isDirectory
then getRecursiveContents path
else return [path]
return (concat paths)
findInFile:: String -> FilePath -> IO Bool
findInFile needle filePath= do
content <- readFile filePath
return (needle `L.isInfixOf` content)
findInFolder:: (String -> Bool) -> FilePath -> String -> IO [IO Bool]
findInFolder p path needle = do
files <- getRecursiveContents path
return (map (findInFile needle) (filter p files))
find = findInFolder (\p -> takeExtension p `elem` [".py", ".xml", ".html"])
I 할 수 있습니다
완벽하지만 폴더에 대한 동일한 검색 할 수없는*Main> findInFile "search_string" "./path/to/a/file"
True
: 내 파일에서
*Main> find "./path/to/a/folder" "search_string"
*Main>
을 시스템 ./path/to/a/file
은 ./path/to/a/folder
에 있습니다. 따라서 나는 동일한 결과를 기대하고 있었다.
내가 뭘 잘못하고 있니?
참고 : getRecursiveContents
은 real world haskell입니다.