2014-05-22 232 views
-1
let minus = function 
    | Int.min_value, _ | Int.max_value, _ | _, Int.min_value | _, Int.max_value -> 0 
    | x, y -> x - y 

Error: Parse error: [module_longident] expected after "." (in [module_longident])这个功能有什么问题?

我看不出有什么问题。

Core.Std这样做在utop打开

回答

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)。