Now that we’ve seen the myriad of ways to match text against regular expressions, let’s turn our attention back to glob patterns. We want to write a function that will take a glob pattern and return its representation as a regular expression. Both glob patterns and regexps are text strings, so the type that our function ought to have seems clear:
-- file: ch08/GlobRegex.hs module GlobRegex ( globToRegex , matchesGlob ) where import Text.Regex.Posix ((=~)) globToRegex :: String -> String
The regular expression that we generate must be anchored so that it starts matching from the beginning of a string and finishes at the end:
-- file: ch08/GlobRegex.hs globToRegex cs = '^' : globToRegex' cs ++ "$"
Recall that the String is
just a synonym for [Char], a list of Chars.
: operator puts a
^ character in this case)
onto the front of a list, where the list is the value returned by the
Haskell does not require that a value or function be declared or defined in a source file before it’s used. It’s perfectly normal for a definition to come after the first place it’s used. The Haskell compiler doesn’t care about ordering at this level. This grants us the flexibility to structure our code in the manner that makes most logical sense to us, rather than follow an order that makes the compiler writer’s life easiest.
Haskell module writers often use this flexibility to ...