Why is lazy evaluation useful?

后端 未结 22 1604
无人共我
无人共我 2020-11-29 17:04

I have long been wondering why lazy evaluation is useful. I have yet to have anyone explain to me in a way that makes sense; mostly it ends up boiling down to \"trust me\".<

22条回答
  •  攒了一身酷
    2020-11-29 17:43

    There's a difference between normal order evaluation an lazy evaluation (as in Haskell).

    square x = x * x
    

    Evaluating the following expression...

    square (square (square 2))
    

    ... with eager evaluation:

    > square (square (2 * 2))
    > square (square 4)
    > square (4 * 4)
    > square 16
    > 16 * 16
    > 256
    

    ... with normal order evaluation:

    > (square (square 2)) * (square (square 2))
    > ((square 2) * (square 2)) * (square (square 2))
    > ((2 * 2) * (square 2)) * (square (square 2))
    > (4 * (square 2)) * (square (square 2))
    > (4 * (2 * 2)) * (square (square 2))
    > (4 * 4) * (square (square 2))
    > 16 * (square (square 2))
    > ...
    > 256
    

    ... with lazy evaluation:

    > (square (square 2)) * (square (square 2))
    > ((square 2) * (square 2)) * ((square 2) * (square 2))
    > ((2 * 2) * (2 * 2)) * ((2 * 2) * (2 * 2))
    > (4 * 4) * (4 * 4)
    > 16 * 16
    > 256
    

    That's because lazy evaluation looks at the syntax tree and does tree-transformations...

    square (square (square 2))
    
               ||
               \/
    
               *
              / \
              \ /
        square (square 2)
    
               ||
               \/
    
               *
              / \
              \ /
               *
              / \
              \ /
            square 2
    
               ||
               \/
    
               *
              / \
              \ /
               *
              / \
              \ /
               *
              / \
              \ /
               2
    

    ... whereas normal order evaluation only does textual expansions.

    That's why we, when using lazy evaluation, get more powerful (evaluation terminates more often then other strategies) while the performance is equivalent to eager evaluation (at least in O-notation).

提交回复
热议问题