这个功能有什么问题?

时间:2014-05-22 10:28:51

标签: ocaml

let minus = function
    | Int.min_value, _ | Int.max_value, _ | _, Int.min_value | _, Int.max_value -> 0
    | x, y -> x - y
  

错误:解析错误:“module_longident”在“。”之后出现。 (在   [module_longident])

我看不出任何错误。

我在utop开启<{1}}的情况下执行了此操作

1 个答案:

答案 0 :(得分:1)

Int.min_valueInt.max_value是值,而不是构造函数(构造函数的名称大写,值的名称不是)。

您不能在模式匹配中使用值,您只能使用构造函数。

好的代码是

let minus (x, y) =
  if x = Int.min_value
  || x = Int.max_value
  || y = Int.min_value
  || y = Int.max_value
  then
    0
  else
    x - y

你的错误代码相当于

let min_value = -1000000
let max_value = 1000000

let minus = function
| min_value, _ | max_value, _ | _, min_value | _, max_value -> 0
| x, y -> x - y

编译,因为它使用正确的名称(不是来自不同模块的名称)但产生错误的结果(总是0)。