How is foldl lazy?

前端 未结 4 2442
星月不相逢
星月不相逢 2021-02-19 16:33

There are lots of good questions and answers about foldl, foldr, and foldl\' in Haskell.

So now I know that:
1) foldl<

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 17:14

    It seems like I have to start worrying about order-of-evaluation in Haskell, if I don't want my program to crash.

    In my opinion, the best options if you want your program not to crash are (ranked from better to worse in terms of yield per effort spent): 1. Give it enough resources. 2. Improve your algorithm. 3. Do micro-optimisations (one of which is foldl').

    So, rather that worrying about the order of evaluation I'd rather first worry about what is to be evaluated (is foldr enough? can I do without folding that at all?). And before that, I'd increase the available stack space.

    You don't limit your whole program to 8MB of RAM, do you? Why would you limit the stack space then? Just increase the stack space to 4GB and start worrying when something really hogs a lot of it (like you do with heap memory).

    And to somewhat answer the question of how foldl is lazy:

    foldl (\x y -> y) undefined [undefined, 8] -- evaluates to 8
    foldl' (\x y -> y) undefined [undefined, 8] -- fails to evaluate
    

提交回复
热议问题