Why does multiplication only short circuit on one side

后端 未结 3 517
独厮守ぢ
独厮守ぢ 2021-02-03 17:39

I was messing around with fix and after messing around with it I came across some weird behavior, namely that 0 * undefined is *** Exception: Pre

3条回答
  •  迷失自我
    2021-02-03 18:25

    Take the following for example

    (if expensiveTest1 then 0 else 2) * (if expensiveTest2 then 0 else 2)
    

    You have to choose a side to evaluate. If expensiveTest2 is an infinite loop, you will never be able to tell whether the right side is 0 or not, so you can't tell whether or not to short circuit the right side, so you never get to look at the left side. You can't check if both sides are 0 at once.

    As for whether you can rely on short circuiting to act in a certain way, just keep in mind that undefined and error acts exactly like an infinite loop as long as you don't use IO. Therefore, you can test short circuiting and laziness using undefined and error. In general, short circuiting behavior varies from function to function. (There are also different levels of laziness. undefined and Just undefined may give different results.)

    See this for more details.

提交回复
热议问题