-1

我在这里查看收集功能列表:https ://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/fsharp-collection-types

但我似乎找不到我要找的东西。

我有一个元素列表:

[ 3; 4; 5; 6; 7; 8 ]

我正在寻找这样的东西:

[ 3; 4; 5; 6; 7; 8 ]
|> List.iterPair (fun x y -> ())

它会经过我的地方:

3 4
4 5
5 6
6 7
7 8

有没有办法用内置函数做到这一点?

4

1 回答 1

0

此操作通常称为pairwise

确实,有List.pairwise,它返回一个元组。

list 
|> List.pairwise
|> List.iter(fun (x, y) -> printfn "%d %d" x y)

给出你预期的输出。

一种更一般的思考方式是:

List.zip (list |> List.skip 1) list

由于在 Linq 中成对不可用:

items.Zip(items.Skip(1), (a, b) => a, b)

当然,使用 a 会更有效scan,但这更具可读性。

于 2020-03-29T13:50:46.597 回答