Proposal: Add functions to get consecutive elements to Data.List

Joachim Breitner mail at joachim-breitner.de
Wed Apr 13 07:53:07 UTC 2016


Hi,

Am Dienstag, den 12.04.2016, 22:55 +0200 schrieb Johan Holmquist:
> I propose adding two new functions to Data.List:
> 
>     zipConsecutives :: [a] -> [(a,a)]
>     zipConsecutives xs = zip xs (tail xs)
> 
>     zipConsecutivesWith :: (a -> a -> b) -> [a] -> [b]
>     zipConsecutivesWith f xs = zipWith f xs (tail xs)
> 
> (with possibly more efficient implementations)

I’m +1 on this. The fact that people debate whether tail or drop 1 is
the right thing to use here point to the fact that this is less trivial
than it looks.

When I have to write it, it takes me a moment to think whether it is "
"zip xs (tail xs)" or " "zip (tail xs) xs".

Also, when _reading_ code, "zip xs (tail xs)" requires more thought
than zipConsecutives.

Furthermore, the standard idiom "zip xs (tail xs)" refers to its
argument multiple times. It is thus not a combinator that you can put
into a chain of function applications. Also, for this reason, if you
compare lenths, then please compare the lengths of
  (\xs -> zip xs (tail xs))
with
  zipConsecutives

And finally, an explicit name for zipConsecutives has the potential for
list-fusion (see #11815 for some discussion in that direction).

Greetings,
Joachim




-- 
Joachim “nomeata” Breitner
  mail at joachim-breitner.dehttps://www.joachim-breitner.de/
  XMPP: nomeata at joachim-breitner.de • OpenPGP-Key: 0xF0FBF51F
  Debian Developer: nomeata at debian.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160413/d21e893b/attachment.sig>


More information about the Libraries mailing list