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
它,但它将当前状态(在给定的情况下,累加器)传递给列表的每个元素.
是的,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)