SML - 函数计算不正确

c-p*_*pid 1 if-statement function sml

对于我大学的课程,我必须学习SML.我现在学习了Java并且遇到了我的SML问题.我有这个功能,应该只为动物园计算入口费.

fun calcEntryFee (erm:bool,dauer:int,dschungel:bool,gebtag:bool):real=
let
    val c = 7.0
in
    if erm then c + 14.50 else c + 19.50;
    if dauer < 120 then c - 4.0 else c;
    if dschungel then c + 1.5 else c;
    if gebtag then c / 2.0 else c
end;
Run Code Online (Sandbox Code Playgroud)

问题是这个函数'返回'7.0或3.5.但似乎没有执行其他3个if语句.

And*_*erg 6

ML中没有语句,只有表达式.甚至A;B是一个表达式,它评估A并且B结果是结果B.因此,您的前3个if表达式的结果将被丢弃.

此外,变量是真正数学意义上的变量,因此它们是不可变的.将程序视为数学公式.

您可能想要写的内容如下:

fun calcEntryFee (erm : bool, dauer : int, dschungel : bool, gebtag : bool) : real =
let
    val fee =
        7.0
        + (if erm then 14.50 else 19.50)
        - (if dauer < 120 then 4.0 else 0.0)
        + (if dschungel then 1.5 else 0.0)
in
    if gebtag then fee / 2.0 else fee
end
Run Code Online (Sandbox Code Playgroud)