Why is lazy evaluation useful?

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

    A useful example of lazy evaluation is the use of quickSort:

    quickSort [] = []
    quickSort (x:xs) = quickSort (filter (< x) xs) ++ [x] ++ quickSort (filter (>= x) xs)
    

    If we now want to find the minimum of the list, we can define

    minimum ls = head (quickSort ls)
    

    Which first sorts the list and then takes the first element of the list. However, because of lazy evaluation, only the head gets computed. For example, if we take the minimum of the list [2, 1, 3,] quickSort will first filter out all the elements that are smaller than two. Then it does quickSort on that (returning the singleton list [1]) which is already enough. Because of lazy evaluation, the rest is never sorted, saving a lot of computational time.

    This is of course a very simple example, but laziness works in the same way for programs that are very large.

    There is, however, a downside to all this: it becomes harder to predict the runtime speed and memory usage of your program. This doesn't mean that lazy programs are slower or take more memory, but it's good to know.

提交回复
热议问题