
Hi all -- Pardon me if this has been answered before: how come there's a stripPrefix in Data.List, but no matching stripSuffix? Thanks! Alvaro

On Tue, Jul 17, 2012 at 8:33 PM, Alvaro Gutierrez
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. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On 18/07/2012, at 12:37 PM, Brandon Allbery wrote:
On Tue, Jul 17, 2012 at 8:33 PM, Alvaro Gutierrez
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.

On Tue, Jul 17, 2012 at 11:34 PM, Richard O'Keefe
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.
Isn't this the case with isSuffixOf, though? And yet it's there along with isPrefixOf...
It's so easy to write
stripSuffix sfx lst = case stripPrefix (reverse sfx) (reverse lst) of Nothing -> Nothing Just ys -> Just (reverse ys)
Sure, it's not difficult to write such a function; the issue is the asymmetry (and thus, broken user expectations) based on the rest of the library.
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.
For what it's worth, there are a lot of other cases (outside of file path handling) in which I've found it useful.

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.
For what it's worth, there are a lot of other cases (outside of file path handling) in which I've found it useful.
I also have 'rdrop' and 'rdropWhile' and 'rstrip' in my stdlib, and use them regularly, if not frequently, along with List.isSuffixOf. Yes, they're inefficient for long lists and hang on infinite ones, but there are still lots of short lists out there.
participants (4)
-
Alvaro Gutierrez
-
Brandon Allbery
-
Evan Laforge
-
Richard O'Keefe