0

The syntax in this language is confusing.

fun bar a =
  print (Int.toString a);
    0

compiles. No idea why emacs indents the 0 though.

fun bar a =
  print (Int.toString a)
    0

Throws an error.

Error: operator is not a function [tycon mismatch]
  operator: unit
  in expression:
    (print (Int.toString a)) 0


fun foo a =
  if a < 0
  then
      0
  else
      0

compiles.

fun foo a =
  if a < 0
  then
      print (Int.toString a);
      0
  else
      0

throws an error.

syntax error: replacing  SEMICOLON with  EQUALOP

Wat?

I can't make any sense of this.

lo tolmencre
  • 3,804
  • 3
  • 30
  • 60

1 Answers1

3

It seems you have trouble understanding where semicolons can be used in SML. There are two main places where they're allowed:

  1. Inside a parenthesized group: (a; b). That means that a; b is not valid. You need to wrap it inside parentheses.

  2. In between in and end in a let block. However, you don't parentheses here:

let
  val foo = ...
in
  a;
  b;
  c
end

So, your last example should be:

fun foo a =
  if a < 0
  then (print (Int.toString a); 0)
  else 0

They can also be used to separate top-level expressions or declarations inside a file or at the REPL, but they're optional for that purpose. It's why your first example compiled.

Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • Part of the confusion might be traceable to the way in which some of the standard books such as Ullman's tend to overuse semicolons. It is easy to get the impression that semicolons are primarily terminators rather than delimiters. – John Coleman Apr 22 '16 at 15:44
  • Is there a way to use the print statement as the last statement in an if / else branch and not return () from that branch? This language feels like a corset... – lo tolmencre Apr 22 '16 at 16:30
  • @lotolmencre `if true then print ("foo") else ()`. The `print` statement is already a unit function. You could always wrap this with another function `fun printIfTrue(test, val) = if test then print(val) else ()`. – eatonphil Apr 22 '16 at 16:33