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.