[Haskell-cafe] Re: File path programme

Krasimir Angelov kr.angelov at gmail.com
Tue Jan 25 07:43:43 EST 2005


On Tue, 25 Jan 2005 13:32:29 +0200, Krasimir Angelov
<kr.angelov at gmail.com> wrote:
> >> What about splitFileExt "foo.bar."? ("foo", "bar.") or ("foo.bar.", "")?
> >
> > The latter makes more sense to me, as an extension of the first case
> > you give and splitting "foo.tar.gz" to ("foo.tar", "gz").
> 
> I will take a look at this. I also don't know which case is more natural.

("foo.bar.", "") is more natural for me because it eleminates the
special case for "." and "..". The original definition of splitFileExt
is:

splitFileExt :: FilePath -> (String, String)
splitFileExt p =
  case pre of
	[]      -> (p, [])
	(_:pre) -> (reverse (pre++path), reverse suf)
  where
    (fname,path) = break isPathSeparator (reverse p)
    (suf,pre) | fname == "." || fname == ".." = (fname,"")
              | otherwise                     = break (== '.') fname

The definition can be changed to:

splitFileExt :: FilePath -> (String, String)
splitFileExt p =
  case break (== '.') fname of
	(suf@(_:_),_:pre) -> (reverse (pre++path), reverse suf)
	_                 -> (p, [])
  where
    (fname,path) = break isPathSeparator (reverse p)

The letter is simplier, it doesn't treat "." and ".." as special cases
and for it
splitFileExt "foo.bar." == ("foo.bar.", "")

Cheers,
  Krasimir


More information about the Haskell-Cafe mailing list