-
Notifications
You must be signed in to change notification settings - Fork 104
/
findseq.hs
34 lines (32 loc) · 1.05 KB
/
findseq.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{-# OPTIONS_GHC -Wall #-}
import System.Directory
import System.FilePath
import System.Environment
import Data.List hiding (find)
-- <<main
main :: IO ()
main = do
[s,d] <- getArgs
r <- find s d
print r
-- >>
-- <<find
find :: String -> FilePath -> IO (Maybe FilePath)
find s d = do
fs <- getDirectoryContents d -- <1>
let fs' = sort $ filter (`notElem` [".",".."]) fs -- <2>
if any (== s) fs' -- <3>
then return (Just (d </> s))
else loop fs' -- <4>
where
loop [] = return Nothing -- <5>
loop (f:fs) = do
let d' = d </> f -- <6>
isdir <- doesDirectoryExist d' -- <7>
if isdir
then do r <- find s d' -- <8>
case r of
Just _ -> return r -- <9>
Nothing -> loop fs -- <10>
else loop fs -- <11>
-- >>