It’s all very well to have a function that
can match glob patterns, but we’d like to be able to put this to
practical use. On Unix-like systems, the glob
function returns the names of all files
and directories that match a given glob pattern. Let’s build a similar
function in Haskell. Following the Haskell norm of descriptive naming,
we’ll call our function namesMatching
:
-- file: ch08/Glob.hs module Glob (namesMatching) where
We specify that namesMatching
is the only name that users of
our Glob
module will be able to see.
This function will obviously have to manipulate filesystem paths a lot, splicing and joining them as it goes. We’ll need to use a few previously unfamiliar modules along the way.
The System.Directory
module provides standard
functions for working with directories and their contents:
-- file: ch08/Glob.hs import System.Directory (doesDirectoryExist, doesFileExist, getCurrentDirectory, getDirectoryContents)
The System.FilePath
module abstracts the details
of an operating system’s path name conventions. The (</>)
function joins two path
components:
ghci>
:m +System.FilePath
ghci>
"foo" </> "bar"
Loading package filepath-1.1.0.0 ... linking ... done. "foo/bar"
The name of the dropTrailingPathSeparator
function is
perfectly descriptive:
ghci>
dropTrailingPathSeparator "foo/"
"foo"
The splitFileName
function splits a path at the
last slash:
ghci>
splitFileName "foo/bar/Quux.hs"
("foo/bar/","Quux.hs")ghci>
splitFileName "zippity"
("","zippity") ...
No credit card required