Reputation: 91
I write two different pieces of code trying to figure out the substitution v.s. evaluation order SML uses with respect to function arguments. But the results confused me a lot.
The toy function is as below
fun pow x y = if y=0 then 1 else x * (pow x y-1));
It only works as I expected when I surround the expression, i.e. y-1 with parens. So I augment it with a simple print statement to track what's going on.
fun pow x y = (print("y is: "^ Int.toString(y)^"\n");
if y = 0 then 1 else x * (pow x y-1))
It runs like value y remains the same every time the function is called( Apparently y-1 is not properly evaluated or parsed as a whole here.)
However, another dummy code I wrote below works perfectly out of my expectation, though.
fun foo x y= x+y;
val res= foo 2 3-9
res = -4 which does not run in the same logic as above.
Any insights on this will be appreciated!!
(I have read some simple illustration about adding parens allows to determine where the argument ends, but it's not so convincing)
Upvotes: 0
Views: 120
Reputation: 370162
pow x y - 1
is equivalent to (pow x y) - 1
. The same is true for your foo example, but 2 + (3 - 9)
and (2 + 3) - 9
actually have the same result, which is why you don't see a difference.
Upvotes: 1