3

In SML I must use the else part , since those are the rules of the language .

How can I do nothing in the else condition then ?

fun calc(input : string ) : int =

    let
      val outStr = ref "someString"
      val outInt = ref 0
    in
      outInt := (validateHelper(input) handle _ => ~1);
      if (outInt <> ~1) 
         then
            (  outStr := replaceRomanDec(input);       (* replace roman number with decimal *)
               outInt := (calcMyRomanExpression(!outStr) handle _ => ~1);
            )
         else (* nada *)

      !outInt
    end;
4

1 回答 1

5
else ()

The value () is the unique inhabitant of the unit type. It makes the whole if-then-else construct well-typed, since the then branch also has type unit.

Finally, in some ML variants if e1 then e2 can be used as shortcut for if e1 then e2 else (), but not in SML.

于 2013-01-06T09:19:07.973 回答