Why is lazy evaluation useful?

后端 未结 22 1602
无人共我
无人共我 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:47

    Consider a tic-tac-toe program. This has four functions:

    • A move-generation function that takes a current board and generates a list of new boards each with one move applied.
    • Then there is a "move tree" function which applies the move generation function to derive all the possible board positions that could follow from this one.
    • There is a minimax function that walks the tree (or possibly only part of it) to find the best next move.
    • There is a board-evaluation function that determines if one of the players has won.

    This creates a nice clear separation of concerns. In particular the move-generation function and the board evaluation functions are the only ones that need to understand the rules of the game: the move tree and minimax functions are completely reusable.

    Now lets try implementing chess instead of tic-tac-toe. In an "eager" (i.e. conventional) language this won't work because the move tree won't fit in memory. So now the board evaluation and move generation functions need to be mixed in with the move tree and minimax logic because the minimax logic has to be used to decide which moves to generate. Our nice clean modular structure disappears.

    However in a lazy language the elements of the move tree are only generated in response to demands from the minimax function: the entire move tree does not need to be generated before we let minimax loose on the top element. So our clean modular structure still works in a real game.

提交回复
热议问题