How do you write the function 'pairs' in Haskell?

前端 未结 4 605
萌比男神i
萌比男神i 2021-02-07 04:50

The pairs function needs to do something like this:

pairs [1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)]
4条回答
  •  甜味超标
    2021-02-07 05:39

    You could go as far as

    import Control.Applicative (<*>)
    pairs = zip <*> tail
    

    but

    pairs xs = zip xs (tail xs)
    

    is probably clearer.

提交回复
热议问题