开发者

Initial state in F# List.scan

I have a simple problem and as I'm an F# newbie I can't seem to figure out how to do this. I have a list of tuples:

let l = [ (a, 2); (b, 3); (c, 2); (d, 6) ]

that I want to transform into this:

let r = [ (a, 2); (b, 5); (c, 7); (d, 13) ]

This simply adds the values of the second element in each tuple: 2 + 3 + 2 + 6. The objects a, b, c and d are complex objects that I simply want to keep.

I thought I should use List.scan for this. It takes a list, threads an accumulator through the开发者_如何学编程 computation and returns a list:

let r = l |> List.scan (fun (_, s) (o, i) -> (o, s + i)) (??, 0) |> List.tail

But I don't know what to fill in for the question marks. I'm not interested in the initial state except for the 0. And I don't want to specify some 'empty' instance of the first tuple element.

Or is there a simpler way of doing this?


You can use first element as an initial state:

let l = [ ("a", 2); ("b", 3); ("c", 2); ("d", 6) ]

let x::xs = l
let res = (x, xs) ||> List.scan (fun (_, x) (o, n) -> o, x + n) // [("a", 2); ("b", 5); ("c", 7); ("d", 13)]

Special case with empty list should be processed separately

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜