dov11020 2019-01-13 09:16
浏览 20
已采纳

如何理解这个递归结果

I wrote a bug while coding, when I solved the problem, I was confused about the output of the code,the code show as below:

type (
    Handler func(name string) error
)

func mh(h Handler) Handler {
    return func(name string) error {
        fmt.Printf("return mh,name=%s,h=%x
", name, h)
        return h(name)
    }
}

func main() {
    var h Handler
    h = func(name string) error {
        fmt.Printf("********************************
")
        fmt.Printf("before func h=%x
", h)
        h = mh(h)
        fmt.Printf("after func h=%x
", h)
        return h(name)
    }
    fmt.Printf("main h=%x
", h)
    h("main")
}

Running the code, the output is:

main h=486d40
********************************
before func h=486d40
after func h=486c00
return mh,name=main,h=486d40
********************************
before func h=486c00
after func h=486c00
return mh,name=main,h=486c00
return mh,name=main,h=486d40
********************************
before func h=486c00
after func h=486c00
return mh,name=main,h=486c00
return mh,name=main,h=486c00
return mh,name=main,h=486d40
.......

I don't understand the call stack.I thought the output should be loop "mh".

  • 写回答

2条回答 默认 最新

  • dongxie3352 2019-01-14 08:31
    关注

    The key thing to understand is that this line:

    h = mh(h)
    

    Does not call the h function. It calls the mh() function, which just returns a function value, but it does not call h() either. If the returned function value would be called, that would call h().

    So the main() function stores a function value in h, then calls h().

    This h() prints "before", then wraps h in another function, and stores the result in h, then prints "after". It's important to know that the wrapper function (the value returned by mh()) is a closure, and it stores the original value of h, so assigning the result to h does not affect the h inside the wrapped function.

    So h ends by calling h which is now the wrapped function. The wrapped function starts by printing "return", then it calls the original, unwrapped h.

    The original, unwrapped h again prints "before", then it wraps the current value of h (which is the wrapped function), stores it in h, then prints "after".

    And then calls h, which is now a 2-times wrapped function. It starts by printing "return", then calls the saved h value, which is a 1-time wrapped function. The 1-time wrapped function starts by printing "return" (again), then proceeds with the original one, which prints "before", wraps h which will now be 3-times wrapped, stores it in h, then calls h (which is the 3-times wrapped function value)...

    This logic continues, the function value stored in h will become wrapped more and more, and the wrapped function always has a saved value of the one-less times wrapped previous function.

    As the "iteration" continues, the "wrapping depth" increases, therefore you'll see more and more "return" statements printed (as that's what the wrapping does).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭