fun temp(x) =
let val a = x
in if a mod (x-1) = 0 then x
else temp(x-1)
end;
this is example Standard ML code.
What I want to know is that
for example user call temp(10); then a = 10 and the temp(x-1) is called.
then the variable ‘a’ will change to 9. Can I keep it as 10 in Standard ML?
If your function contains
val a = x, then for any invocation oftemp(x), the value ofawill be equal to the value ofx. If you want to remember the value ofafrom a previous invocation when you recurse, you need to pass it around as a parameter like this:You could also make the helper function an inner function. If you do that, you can actually remove
aas a parameter and instead close overalike this: