Before we begin, a small note: I’ll be on conference in Istanbul next week, so I won’t be here for the next two exercises.
In today’s Programming Praxis exercise we’re supposed to implement grep based on the regex matching functions we wrote in previous exercises. Let’s get started.
First, some imports.
import System.Environment import Text.Printf
Obviously, we're going to need the code we wrote in the previous two exercises.
--Code from the previous two exercises goes here
grepString shows all the lines of the input that (don't) contain the regular expression, prefixed by the filename if one is provided.
grepString :: Bool -> Maybe String -> [Chunk] -> String -> IO () grepString b p cs = mapM_ (printf "%s%s\n" $ maybe "" (++ ": ") p) . filter ((== b) . match cs) . lines
grep searches all the given files, or the standard input if none are provided, for the regular expression.
grep :: Bool -> [String] -> IO () grep _  = error "Not enough arguments provided" grep b (r:ps) = either print (f ps) $ parseRegex r where f  cs = grepString b Nothing cs =<< getLine f _ cs = mapM_ (\p -> grepString b (Just p) cs =<< readFile p) ps
Finally, main checks whether to display the lines that do or do not match.
main :: IO () main = f =<< getArgs where f ("-v":args) = grep False args f args = grep True args
And that's it. See you guys in a week.