Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Forked from anonymous/gist:615e48004ca2eed82d0a
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonymorris/a0e82e603d4d32597bbc to your computer and use it in GitHub Desktop.
Save tonymorris/a0e82e603d4d32597bbc to your computer and use it in GitHub Desktop.
import System.Environment
import System.IO
import Control.Monad (when)
matchGlob :: String -> String -> Bool
matchGlob = undefined
main :: IO ()
main = do
args <- getArgs
case args of
[] -> putStrLn "usage: glob <glob-pattern> [file 1] [file 2] ... [file N]"
[a] -> stdinglob a
(a:as) -> mapM_ (fileglob a) as
where
stdinglob glob = do
string <- getLine
when (matchGlob glob string) $ putStrLn string
stdinglob glob
fileglob glob fileName = do
withFile fileName ReadMode $ \fileHandle -> do
contents <- hGetContents fileHandle
let matches = filter (matchGlob glob) . words $ contents
mapM_ (\match -> putStrLn $ fileName ++ ": " ++ match) matches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment