【发布时间】:2016-05-15 03:28:34
【问题描述】:
如何获取子列表
[1]; [1; 2]; [1; 2; 3]; ...; [1; 2; 3; ...; n]
来自列表
[1; 2; 3; ...; n]
以最惯用的方式?我所能做的就是:
List.scan (fun acc elem -> elem::acc) [] [1;2;3;4;5]
> val it : int list list =
[[]; [1]; [2; 1]; [3; 2; 1]; [4; 3; 2; 1]; [5; 4; 3; 2; 1]]
谢谢。
【问题讨论】:
-
不知道是否有惯用的方式,但你的方式很好(只需添加一个地图来反转列表)-顺便说一句:它被称为
heads;) -
子列表要多得多;)
-
我说的是
List.scan (fun acc elem -> elem::acc) [] [1;2;3;4;5] |> List.map List.rev |> List.tail -
哎呀我的坏...它可能被称为 inits 而不是 *heads*
标签: list f# linked-list