F# - 运算符优先级

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

运算符 关联性
as Right
when Right
| (pipe) Left
; Right
let Non associative
function, fun, match, try Non associative
if Non associative
Right
:= Right
, Non associative
or, || Left
&, && Left
< op, >op, =, |op, &op Left
&&& , |||, ^^^, ~~~, <<<, >>> Left
^ op Right
:: Right
:?>, :? Non associative
- op, +op, (binary) Left
* op, /op, %op Left
** op Right
f x (function application) Left
| (pattern match) Right
prefix operators (+op, -op, %, %%, &, &&, !op, ~op) Left
. Left
f(x) Left
f<types> Left

示例

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.html