寻找类似于 map 的 F# 库函数,它将传递给 f 计算的当前状态
我想知道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
用法:
let xxx = map_acc id [1..10]
val xxx : int list = [1; 3; 6; 10; 15; 21; 28; 36; 45; 55]
它的用途与 map
非常相似,但它将当前状态(在给定情况下为累加器)传递给列表的每个元素。
I wonder if there is some function in the F# libraries similar to this one?
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
Usage:
let xxx = map_acc id [1..10]
val xxx : int list = [1; 3; 6; 10; 15; 21; 28; 36; 45; 55]
Its purpose is quite similar to map
's but it passes the current state (in the given case, an accumulator) to each element of the list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,List.scan 是您寻找的缺失密钥:
Yes, List.scan is the missing key you seek: