理解递归调用和函数调用

时间:2013-11-19 09:26:27

标签: f#

在询问question其中一位用户是否愿意给我这个答案后,我很难理解它。

member private this.MinMaxAlphaBeta (board : Board, isMin : bool, alpha : int, beta : int) : int =
    let point = this.BoardPoint (board)
    if point <> -2 then point
    else
        let UpdateAlphaBeta x alpha beta =
            match x with
            | 10 ->
                if isMin then
                    beta, alpha, beta
                else
                    alpha, alpha, beta
            | _ ->
                if isMin && x < beta then
                    x, alpha, x
                elif not isMin && x > alpha then
                    x, x, beta
                else
                    x, alpha, beta

        let rec loop x alpha beta i =
            if i > 8 then x
            else
                let x', alpha', beta' =
                    let x =
                        let b = Board (board)
                        if b.SetBoardBool i then
                            // NOTE : This is a _recursive_ call!
                            this.MinMaxAlphaBeta (b, not isMin, alpha, beta)
                        else 10

                    UpdateAlphaBeta x alpha beta

                let x_new =
                    if isMin then min x x' else max x x'

                loop x_new alpha' beta' (i + 1)

        let x_initial = 0
        loop x_initial alpha beta 0   // Start at the zero-th element.
  1. let UpdateAlphaBeta x alpha beta这个函数如何将结果返回给调用者?

  2. let rec loop x alpha beta i =有另一个函数let x', alpha', beta'从哪里调用?

  3. min x x' else max x x'了解x和x'

  4. 之间的区别

1 个答案:

答案 0 :(得分:2)

  1. F#对let的最后一行有隐含的回报,因此所有标记的行都是潜在的返回值

    let UpdateAlphaBeta x alpha beta =
        match x with
        | 10 ->
            if isMin then
                beta, alpha, beta //return
            else
                alpha, alpha, beta //return
        | _ ->
            if isMin && x < beta then
                x, alpha, x //return
            elif not isMin && x > alpha then
                x, x, beta  //return
            else
                x, alpha, beta //return
    
  2. x',alpha',beta'基本上执行let a,b,c = 1,2,3的复杂版本

  3. min x x'的最低值为x,而x'的最低值为2)