Why recursive `let` make space effcient?

前端 未结 3 864
鱼传尺愫
鱼传尺愫 2020-12-04 23:50

I found this statement while studying Functional Reactive Programming, from \"Plugging a Space Leak with an Arrow\" by Hai Liu and Paul Hudak ( page 5) :

<
3条回答
  •  情话喂你
    2020-12-05 00:02

    Put simply, variables are shared, but function applications are not. In

    repeat x = x : repeat x
    

    it is a coincidence (from the language's perspective) that the (co)recursive call to repeat is with the same argument. So, without additional optimization (which is called static argument transformation), the function will be called again and again.

    But when you write

    repeat x = let xs = x : xs in xs
    

    there are no recursive function calls. You take an x, and construct a cyclic value xs using it. All sharing is explicit.

    If you want to understand it more formally, you need to familiarize yourself with the semantics of lazy evaluation, such as A Natural Semantics for Lazy Evaluation.

提交回复
热议问题