I came across an interesting question from SICP, and noticed some subtle fact. There is the well-known operator
?: in C++ and several other languages. Now think of a function which takes 3 arguments and performs the conditional operator on them:
Is this function equivalent to
Here is the C++ illustration of the question I mentioned above. The code is simply computing a square root using Newton’s method. See what happens if you use the commented statement to replace the return statement above it.
The reason behind this hard-to-foresee behavior is: a function always evaluates all its parameters before execution, while
?: does not.