寻找类似于map的F#库函数,它将传递给f当前的计算状态

dev*_*ium 1 .net f# functional-programming

我想知道F#库中是否有类似于这个的功能?

let map_acc (f:int->int) (list:int list) =
  let rec map_acc' f acc = function
    | []   -> []
    | h::t -> (f (h+acc))::(map_acc' f (h+acc) t)
  map_acc' f 0 list
Run Code Online (Sandbox Code Playgroud)

用法:

let xxx = map_acc id [1..10]

val xxx : int list = [1; 3; 6; 10; 15; 21; 28; 36; 45; 55]
Run Code Online (Sandbox Code Playgroud)

它的目的非常类似于map它,但它将当前状态(在给定的情况下,累加器)传递给列表的每个元素.

Ste*_*sen 6

是的,List.scan是您寻找的缺失密钥:

[1..10]
|> List.scan (+) 0
|> List.tail //skip the seed value, 0
|> List.map id //of course, map id is not useful, but just illustration here
Run Code Online (Sandbox Code Playgroud)