print语句导致错误?

时间:2016-04-22 12:03:36

标签: syntax sml smlnj

这种语言的语法令人困惑。

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

编译。不知道为什么emacs会缩进0。

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

引发错误。

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

编译。

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

抛出错误。

syntax error: replacing  SEMICOLON with  EQUALOP

笏?

我无法理解这一点。

1 个答案:

答案 0 :(得分:3)

您似乎无法理解SML中可以使用分号的位置。他们被允许有两个主要的地方:

  1. 括号内的组:(a; b)。这意味着a; b无效。你需要将它包装在括号内。

  2. in块中的endlet之间。但是,这里没有括号:

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

    所以,你的最后一个例子应该是:

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

    它们也可以用于分隔文件内部或REPL中的顶级表达式或声明,但它们是可选的。这就是你编写第一个例子的原因。