Pairing adjacent list items in Haskell

前端 未结 3 626
情话喂你
情话喂你 2020-12-16 00:55

I have a chained list like

[\"root\", \"foo\", \"bar\", \"blah\"]

And I\'d like to convert it to a list of tuples, using adjacent pairs. Li

3条回答
  •  生来不讨喜
    2020-12-16 01:32

    One possible solution:

    pairs [] = []
    pairs (x:[]) = []
    pairs (x:y:zs) = (x, y) : pairs (y : zs)
    

    Definitely not as small as yours, and can probably be optimized quite a bit.

提交回复
热议问题