if statement - SML - Function calculates incorrectly -
for course @ university have learn sml. learnd java befor , having problems sml now. have function should calculates entryfee zoo.
fun calcentryfee (erm:bool,dauer:int,dschungel:bool,gebtag:bool):real= let val c = 7.0 in if erm c + 14.50 else c + 19.50; if dauer < 120 c - 4.0 else c; if dschungel c + 1.5 else c; if gebtag c / 2.0 else c end;
the problem function 'returns' 7.0 or 3.5. doesn't seem execute other 3 if-statements.
there no statements in ml, expressions. a;b
expression, evaluates a
, b
, result result of b
. consequently, result of first 3 if-expressions thrown away.
also, variables variables in true math sense, immutable. think of program mathematical formula.
what want write following:
fun calcentryfee (erm : bool, dauer : int, dschungel : bool, gebtag : bool) : real = let val fee = 7.0 + (if erm 14.50 else 19.50) - (if dauer < 120 4.0 else 0.0) + (if dschungel 1.5 else 0.0) in if gebtag fee / 2.0 else fee end
Comments
Post a Comment