如何将Int列表转换为表示连续范围的元组列表

时间:2014-11-11 20:29:01

标签: haskell

我想转一个这样的列表

[0,10,100,500,1000,5000]

进入表示连续范围的元组列表,如下所示:

[(0,10),(10,100),(100,500),(500,1000),(1000,5000)]

在Haskell中有一种紧凑的方法吗?

2 个答案:

答案 0 :(得分:13)

不确定

fn :: [a] -> [(a, a)]
fn xs = zip xs (tail xs)

Prelude> fn [0, 10, 100, 500]
[(0,10),(10,100),(100,500)]

答案 1 :(得分:0)

这看起来非常紧凑:

consecutivePairs :: [a] -> [(a, a)]
consecutivePairs [] = []
consecutivePairs [_] = []
consecutivePairs (x:xs@(y:_)) = (x, y) : consecutivePairs xs