[Haskell-cafe] stripSuffix

Richard O'Keefe ok at cs.otago.ac.nz
Wed Jul 18 05:34:45 CEST 2012

On 18/07/2012, at 12:37 PM, Brandon Allbery wrote:

> On Tue, Jul 17, 2012 at 8:33 PM, Alvaro Gutierrez <radical at google.com> wrote:
> Pardon me if this has been answered before: how come there's a
> stripPrefix in Data.List, but no matching stripSuffix?
> Probably because prefixes are easier to do, given the nature of singly linked lists. 

Here are two other possible reasons.

It's not just easier, stripPrefix pfx lst is *possible* as long
as pfx is finite, even when lst is infinite.  The same would not
be true of a suffix stripper.

It's so easy to write

stripSuffix sfx lst =
  case stripPrefix (reverse sfx) (reverse lst) of
    Nothing -> Nothing
    Just ys -> Just (reverse ys)

I can think of two cases where I'd want something like this.
One is manipulating file extensions, where I'd want to use
System.FilePath.splitExtension or something like that anyway.
The other is suffix stripping for text processing, where I'd
want to use a trie to match a whole lot of possible suffixes.

More information about the Haskell-Cafe mailing list