F# - 算術運算子



下表顯示了 F# 語言支援的所有算術運算子。假設變數 A 為 10,變數 B 為 20,則 -

運算子 描述 示例
+ 將兩個運算元相加 A + B 將得到 30
- 從第一個運算元中減去第二個運算元 A - B 將得到 -10
* 將兩個運算元相乘 A * B 將得到 200
/ 將分子除以分母 B / A 將得到 2
% 模運算子,整數除法後的餘數 B % A 將得到 0
** 指數運算子,將一個運算元提升到另一個運算元的冪 B**A 將得到 2010

示例

let a : int32 = 21
let b : int32 = 10

let mutable c = a + b
printfn "Line 1 - Value of c is %d" c

c <- a - b;
printfn "Line 2 - Value of c is %d" c

c <- a * b;
printfn "Line 3 - Value of c is %d" c

c <- a / b;
printfn "Line 4 - Value of c is %d" c

c <- a % b;
printfn "Line 5 - Value of c is %d" c

編譯並執行程式後,將產生以下輸出 -

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
fsharp_operators.htm
廣告

© . All rights reserved.