F# - 运算符优先级



下表显示了 F# 语言中运算符和其他表达式关键字的优先级顺序,从最低优先级到最高优先级。

运算符 结合性
as 右结合
when 右结合
| (管道) 左结合
; 右结合
let 非结合
function, fun, match, try 非结合
if 非结合
右结合
:= 右结合
, 非结合
or, || 左结合
&, && 左结合
< op, >op, =, |op, &op 左结合
&&& , |||, ^^^, ~~~, <<<, >>> 左结合
^ op 右结合
:: 右结合
:?>, :? 非结合
- op, +op, (二元) 左结合
* op, /op, %op 左结合
** op 右结合
f x (函数应用) 左结合
| (模式匹配) 右结合
前缀运算符 (+op, -op, %, %%, &, &&, !op, ~op) 左结合
. 左结合
f(x) 左结合
f<types> 左结合

示例

let a : int32 = 20
let b : int32 = 10
let c : int32 = 15
let d : int32 = 5

let mutable e : int32 = 0
e <- (a + b) * c / d // ( 30 * 15 ) / 5
printfn "Value of (a + b) * c / d is : %d" e

e <- ((a + b) * c) / d // (30 * 15 ) / 5
printfn "Value of ((a + b) * c) / d is : %d" e

e <- (a + b) * (c / d) // (30) * (15/5)
printfn "Value of (a + b) * (c / d) is : %d" e

e <- a + (b * c) / d // 20 + (150/5)
printfn "Value of a + (b * c) / d is : %d" e

编译并执行程序后,将产生以下输出:

Value of (a + b) * c / d is : 90 
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90 
Value of a + (b * c) / d is : 50
fsharp_operators.htm
广告

© . All rights reserved.