syntax - nested let and if statements -
i having issues understanding sml's syntax nested let
s , if
s. example, why syntactically ill-formed?
fun stuff = let val p = 2 in if = 1 let val r = p in = r end else 0 end
if a
1 make a
equal tor
, if a
not 1, not create r
in first place , return 0. kinda stupid, o well...
so, why syntactically incorrect?
it not syntactically incorrect. type error.
the problem both branches of if
need have same type, whereas then
-branch has type bool
(a = b
comparison, not assignment, if assignment, it'd have type unit
, wrong type) , else
-branch has type int
. can make code compile returning boolean in else branch this:
fun stuff = let val p = 2 in if = 1 let val r = p in = r end else false end
ps: note can assign ref
s in sml, if wanted assign a
in code, you'd need make int ref
rather plain int
.
Comments
Post a Comment