Proposal: Make a very slight change to the semantics of Data.List.isSuffixOf
David Feuer
david.feuer at gmail.com
Sat Oct 11 12:47:20 UTC 2014
It can be, but your way traverses the entire haystack *twice*. The memory
needs are equivalent to the reversing version, which I consider
unacceptable.
On Sat, Oct 11, 2014 at 5:57 AM, Andreas Abel <abela at chalmers.se> wrote:
> David, the essence of your idea seems mutually drop the correct number of
> elements from needle and hay and then compare for equality. Here is a
> concise implementation of your idea in terms of drop:
>
> isSuffixOf :: forall a . (Eq a) => [a] -> [a] -> Bool
> [] `isSuffixOf` _ = True
> xs `isSuffixOf` ys = xs == drop (length (drop (length xs) ys)) ys
>
> This can be further simplified to
>
> isSuffixOf :: forall a . (Eq a) => [a] -> [a] -> Bool
> [] `isSuffixOf` _ = True
> xs `isSuffixOf` ys = xs == drop (length ys - length xs) ys
>
> which is a very easy to understand program, I think, without need to
> reverse lists.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20141011/3b68f7df/attachment.html>
More information about the Libraries
mailing list