<div dir="ltr">+1 for uncons, I've wanted that a few times.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jul 19, 2014 at 11:47 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">Alexander Berntsen indicates that he has a branch all ready to go. Henning Thienemann seems to prefer the term viewL. Alexander says that Edward Kmett says that uncons/unsnoc is the more common term. Personally, I find uncons and unsnoc to be more intuitive names. Details:</p>


<p dir="ltr">uncons :: [a] -> Maybe (a, [a])<br>
uncons [] = Nothing<br>
uncons (a:as) = Just (a,as)</p>
<p dir="ltr">-- Henning's implementation of "viewR", renamed, looks like</p>
<p dir="ltr">unsnoc :: [a] -> Maybe ([a], a)<br>
unsnoc = foldr (\x -> Just . forcePair . maybe ([],x) (mapFst (x:))) Nothing</p>
<p dir="ltr">I wonder if it would be possible to tease that apart a bit to get the maybe out of the loop, which I think might be prettier. The really tricky part of unsnoc is making it lazy enough—messing around with it a bit suggested that it's very easy to mess up the pair lifting. The tough rule seems to be this mouthful, if I'm not mistaken:</p>


<p dir="ltr">unsnoc (xs ++ (y : _|_)) = Just (xs ++ _|_, _|_)</p><span class="HOEnZb"><font color="#888888">
<p dir="ltr">David</p>
</font></span><br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">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><br></div>