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.