我在学.这是我发现奇怪的事情:
let test_treeways x = match x with
| _ when x < 0 -> -1
| _ when x > 0 -> 1
| _ -> 0;;
Run Code Online (Sandbox Code Playgroud)
如果我然后这样称呼它:
test_threeways -10;;
Run Code Online (Sandbox Code Playgroud)
我会得到类型不匹配错误(因为,据我所知,它解释一元减去好像它是部分函数应用程序,所以它考虑表达式的类型int -> int.但是,这:
test_threeways (-10);;
Run Code Online (Sandbox Code Playgroud)
按预期行事(虽然这实际上计算了值,但据我所知,它不会向函数传递常数"减十".
那么,你如何在OCaml中编写常数负数?
您需要将其括起来以避免解析歧义."test_threeways -10"也可能意味着:从test_threeways中减去10.
并且没有涉及功能应用程序.只需重新定义一元减号,即可看出差异:
#let (~-) = (+) 2 ;; (* See documentation of pervarsives *)
val ( ~- ) : int -> int = <fun>
# let t = -2 ;;
val t : int = -2 (* no function application, constant negative number *)
# -t ;;
- : int = 0 (* function application *)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3365 次 |
| 最近记录: |