【发布时间】:2018-10-01 11:56:40
【问题描述】:
我写了这个little function,为了便于参考,我在这里重复一遍:
/// Take a list of lists, go left-first, and return each combination,
/// then apply a function to the resulting sublists, each length of main list
let rec nestedApply f acc inp =
match inp with
| [] -> f acc
| head::tail ->
[
for x in head do
yield! nestedApply f (x::acc) tail
]
这让我想知道在这种情况下使用 yield! 是否是尾递归的,或者通常与列表推导一起使用。我实际上认为不是,这使得上述函数会创建一个等于主列表大小的堆栈深度。
如果不是,我如何以尾递归的方式编写相同的代码?我已经尝试使用List.collect(在提到的问题中提出了一个想法),但我并没有完全做到。
【问题讨论】:
标签: list f# yield tail-recursion computation-expression