Recursion 为什么不是';这不是F#码尾递归吗?

Recursion 为什么不是';这不是F#码尾递归吗?,recursion,f#,tail-recursion,Recursion,F#,Tail Recursion,我正在查看Visual Studio 2015提供的F#“教程”模板中的代码,我看到了这段代码;我想知道为什么第一个函数不是尾部递归的;我想我理解,但我想确认: /// Computes the sum of a list of integers using recursion. let rec sumList xs = match xs with | [] -> 0 | y::ys -> y + sumList ys /// Make the fun

我正在查看Visual Studio 2015提供的F#“教程”模板中的代码,我看到了这段代码;我想知道为什么第一个函数不是尾部递归的;我想我理解,但我想确认:

/// Computes the sum of a list of integers using recursion.
let rec sumList xs =
    match xs with
    | []    -> 0
    | y::ys -> y + sumList ys

/// Make the function tail recursive, using a helper function with a result accumulator
let rec private sumListTailRecHelper accumulator xs =
    match xs with
    | []    -> accumulator
    | y::ys -> sumListTailRecHelper (accumulator+y) ys
因为“+”是一个函数,并且它的两个参数是先求值的,所以第一个参数不是尾部递归的吗?因此,实际的求值顺序是:y,然后是sumList ys,然后是+?而在第二种情况下,求值顺序是:累加器,y,+然后是sumListTailRecHelper(..)

如果递归调用返回后没有什么事情可做,则调用是尾部递归的。因此,最后一个调用相当于返回到函数代码的开头,并修改了参数


在第一个函数中,您仍然需要将
y
添加到结果中。

是的,这是正确的<第一个代码示例中的code>+是在非空列表分支中计算的最后一个函数