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 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式