<p dir="ltr">I accidentally hit "send" before finishing this message. I *think* my version is probably a little easier to understand than Milan's but it also seems possible that Milan's could take the lead with some more inspired variable names. I also wish I knew a way to avoid the bogus pattern in skip_and_compare (and my equivalent), but I haven't been able to do so.</p>
<div class="gmail_quote">On Oct 10, 2014 12:23 PM, "David Feuer" <<a href="mailto:david.feuer@gmail.com">david.feuer@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">I wouldn't say calling it on a possibly infinite list would necessarily be the wisest thing, but supporting an infinite "needle" with a finite "haystack" comes for free with the forward traversal. Unlike inInfixOf, the extra implementation flexibility gained by specifying that the needle is finite has no serious potential to allow fancy algorithms to improve efficiency.</p>
<p dir="ltr">The real odd semantic duck, I think, is the special case for an empty needle, because it is lazier than one might expect. In particular, I would expect that isSuffixOf [] undefined would be False, because undefined is not a []-terminated list. Leaving *that* unspecified would make a lot of sense to me.</p>
<p dir="ltr">As for simplicity of form, orb__ came up with a version that uses Either instead of Maybe to take advantage of the similarity of two phases to get away with just one helper function, but I think that approach is much harder to understand.</p>
<div class="gmail_quote">On Oct 10, 2014 10:48 AM, "John Lato" <<a href="mailto:jwlato@gmail.com" target="_blank">jwlato@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">I think David's proposed definition is the easiest to read form with good performance so far.</p>
<p dir="ltr">As to definedness, the change in semantics doesn't matter to me.  But I think calling isSuffixOf on possibly infinite lists is a bad idea anyway.</p>
<p dir="ltr">John L.</p>
<p dir="ltr">On Oct 10, 2014 5:35 AM, "Milan Straka" <<a href="mailto:fox@ucw.cz" target="_blank">fox@ucw.cz</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> > -----Original message-----<br>
> > From: David Feuer <<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>><br>
> > Sent: 10 Oct 2014, 05:23<br>
> ><br>
> > On Fri, Oct 10, 2014 at 5:18 AM, Jon Fairbairn <<a href="mailto:jon.fairbairn@cl.cam.ac.uk" target="_blank">jon.fairbairn@cl.cam.ac.uk</a>><br>
> > wrote:<br>
> ><br>
> > > That seems rather wordy to me. Can we get anywhere starting with<br>
> > ><br>
> > ><br>
> > It is rather wordy; I just don't know how to do better.<br>
><br>
> What about something like the following? It uses exactly the same<br>
> algorithm, it just avoids the maybe combinator and Maybe internal<br>
> result, and renames stuff:<br>
><br>
> isSuffixOf :: (Eq a) => [a] -> [a] -> Bool<br>
> [] `isSuffixOf` _ = True<br>
> needle `isSuffixOf` hay = subtract_and_compare needle hay<br>
>   where subtract_and_compare [] diff = skip_and_compare diff hay<br>
>         subtract_and_compare _ [] = False -- needle longer than hay<br>
>         subtract_and_compare (n:ns) (h:hs) = subtract_and_compare ns hs<br>
><br>
>         skip_and_compare [] suffix = needle == suffix<br>
>         skip_and_compare _ [] = True -- first argument must also be []<br>
>         skip_and_compare (d:ds) (s:ss) = skip_and_compare ds ss<br>
><br>
> The redundant skip_and_compare case is there just for completeness and<br>
> readability.<br>
><br>
> Cheers,<br>
> Milan<br>
> _______________________________________________<br>
> Libraries mailing list<br>
> <a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
</p>
<br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
<br></blockquote></div>
</blockquote></div>