[Haskell-cafe] Path names (again?)

Glynn Clements glynn.clements at virgin.net
Thu Feb 5 18:13:55 EST 2004


Vincenzo aka Nick Name wrote:

> I am so often in need for this (I suppose, non-existing) module, that I 
> am going crazy :) I have found discussions in the archives for haskell 
> mailing lists, but is there any, even posix-only, even unix-only, but 
> better portable, implementation of functions to 
> 
> - concatenate file paths

	(concat . intersperse "/")

Maybe you wanted something more, e.g. canonicalisation?

> - tell if a path is absolute or relative

	((== '/') . head)

> - chase symlinks

I was going to ask what you meant here but, AFAICT, Haskell (at least,
GHC 5.04) doesn't appear to recognise the existence of symlinks. So,
whatever you meant, the answer is probably "no".

> - find all the files in a directory (yes, that's what I need :))

Define "file" (e.g. "regular file", "anything other than a directory",
"directory entry" etc). Also, define "in"; i.e. are you talking about
a recursive search (like "find")?

Simplest case:

	getDirectoryContents

If you only want files, as defined by doesFileExist:

	getDirectoryFiles dir = do
		entries <- getDirectoryContents dir
		filterM (doesFileExist . ((dir ++ "/") ++)) entries

Recursion is a somewhat harder (maybe even impossible), given that
doesDirectoryExist doesn't distinguish between a directory and a
symlink to a directory, and the Posix module doesn't appear to include
anything which would be of use (e.g. a binding for lstat()).

-- 
Glynn Clements <glynn.clements at virgin.net>


More information about the Haskell-Cafe mailing list