4

这个 F# seq 表达式在我看来是尾递归的,但我遇到了堆栈溢出异常(启用了尾调用)。有人知道我错过了什么吗?

let buildSecondLevelExpressions expressions =
    let initialState = vector expressions |> randomize
    let rec allSeq state = seq {
        for partial in state do
            if count partial = 1
            then yield Seq.head partial
            if count partial > 1 || (count partial = 1 && depth (Seq.head partial) <= MAX_DEPTH) then
                let allUns = partial
                                |> pick false 1
                                |> Seq.collect (fun (el, rr) -> (createExpUnaries el |> Seq.map (fun bn -> add rr bn)))
                let allBins = partial  // Careful: this case alone produces result recursivley only if |numbers| is even (rightly!).
                                |> pick false 2
                                |> Seq.collect (fun (el, rr) -> (createExpBinaries el |> Seq.map (fun bn -> add rr bn)))
                yield! allSeq (interleave allBins allUns)
    }
    allSeq initialState

如果您想知道,尽管它不应该很重要,但它pick用于生成序列中的元素组合并interleave交错来自 2 个序列的元素。vector是 a 的构造函数ResizeArray

4

3 回答 3

4

正如 Gideon 指出的那样,这不是尾递归,因为“状态”列表中还有其他元素要处理。使这个尾递归并不简单,因为您需要一些应该处理的元素队列。

以下伪代码显示了一种可能的解决方案。我添加work了存储剩余工作的参数。在每次调用中,我们只处理第一个元素。所有其他元素都添加到队列中。完成后,我们从队列中挑选更多工作:

let rec allSeq state work = seq { 
    match state with 
    | partial::rest -> 
        // Yield single thing to the result - this is fine
        if count partial = 1 then yield Seq.head partial 
        // Check if we need to make more recursive calls...
        if count partial > 1 || (* ... *) then 
            let allUns, allBins = // ...
            // Tail-recursive call to process the current state. We add 'rest' to 
            // the collected work to be done after the current state is processed
            yield! allSeq (interleave allBins allUns) (rest :: work)
        else
            // No more processing for current state - let's take remaining
            // work from the 'work' list and run it (tail-recursively)
            match work with 
            | state::rest -> yield! allSeq state rest
            | [] -> () //completed
    | _ -> 
        // This is the same thing as in the 'else' clause above. 
        // You could use clever pattern matching to handle both cases at once
        match work with 
        | state::rest -> yield! allSeq state rest
        | [] -> () } //completed
于 2010-07-16T14:45:26.103 回答
3

我找不到序列表达式中哪些调用在 F# 中位于尾部位置的定义,因此我强烈建议不要编写依赖于当前实现语义的代码,即这是未定义的行为。

例如,尝试枚举(例如应用Seq.length)以下序列会导致堆栈溢出:

let rec xs() = seq { yield! xs() }

但是,正如 Tomas 所指出的,以下方法确实有效:

let rec xs n = seq { yield n; yield! xs(n+1) }

我的建议是始终将递归序列表达式Seq.unfold替换为。在这种情况下,您可能希望累积要完成的工作(例如,当您递归到左分支时,您将右分支压入累加器中的堆栈)。

FWIW,即使是 F# 语言参考也会出错。它提供了以下用于展平树的代码:

type Tree<'a> =
   | Tree of 'a * Tree<'a> * Tree<'a>
   | Leaf of 'a

let rec inorder tree =
    seq {
      match tree with
          | Tree(x, left, right) ->
               yield! inorder left
               yield x
               yield! inorder right
          | Leaf x -> yield x
    } 

他们自己的代码在左侧输入一棵深树时,会因堆栈溢出而终止 F# 交互。

于 2010-07-17T19:33:23.100 回答
1

这不会是尾递归,因为您可能会多次递归调用。转换为伪代码:

allSeq(state)
{
    foreach (partial in state)
    {
        if (...)
        {
            yield ...
        }
        if (...)
        {
            ...
            //this could be reached multiple times
            yield! allSeq(...)
        }
    }
}
于 2010-07-16T13:41:49.893 回答