运算符是一个符号,告诉编译器执行特定的数学或逻辑操作, F#内置丰富的运算符,并提供以下类型的运算符-
下表显示了F#语言支持的所有算术运算符。假设变量A=10,变量B=20,则-
操作符 | 说明 | 示例 |
---|---|---|
+ | 添加两个操作数 | A + B=30 |
- | 从第一个中减去第二个操作数 | A-B=-10 |
* | 将两个操作数相乘 | A * B=200 |
/ | 按分子除分子 | B/A=2 |
% | 模运算符和整数除后的余数 | B%A=0 |
** | 幂运算符,将操作数提高到另一个的幂 | B ** A=20 10 |
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
下表显示了F#语言支持的所有比较运算符,这些二进制比较运算符可用于整数和浮点类型,这些运算符返回布尔类型的值。
假设变量A=10,变量B=20,则-
Operator | Remark | Example |
---|---|---|
== | 相等 | (A == B) is not true. |
<> | 不相等 | (A <> B) is true. |
> | 大于 | (A > B) is not true. |
< | 小于 | (A < B) is true. |
>= | 大于或等于 | (A >= B) is not true. |
<= | 小于或等于 | (A <= B) is true. |
let mutable a : int32 = 21 let mutable b : int32 = 10 if (a = b) then printfn "Line 1 - a is equal to b" else printfn "Line 1 - a is not equal to b" if (a < b) then printfn "Line 2 - a is less than b" else printfn "Line 2 - a is not less than b" if (a > b) then printfn "Line 3 - a is greater than b" else printfn "Line 3 - a is not greater than b" (* Lets change value of a and b *) a <- 5 b <- 20 if (a <= b) then printfn "Line 4 - a is either less than or equal to b" else printfn "Line4 - a is a is greater than b"
编译并执行程序时,将产生以下输出-
Line 1 - a is not equal to b Line 2 - a is not less than b Line 3 - a is greater than b Line 4 - a is either less than or equal to b
下表显示了F#语言支持的所有布尔运算符。假设变量A= true ,变量B= false,然后-
Operator | Remark | Example |
---|---|---|
&& | 布尔AND运算符 | (A && B) is false. |
|| | 布尔OR运算符 | (A || B) is true. |
not | 布尔NOT运算符 | not (A && B) is true. |
let mutable a : bool = true; let mutable b : bool = true; if ( a && b ) then printfn "Line 1 - Condition is true" else printfn "Line 1 - Condition is not true" if ( a || b ) then printfn "Line 2 - Condition is true" else printfn "Line 2 - Condition is not true" (* lets change the value of a *) a <- false if ( a && b ) then printfn "Line 3 - Condition is true" else printfn "Line 3 - Condition is not true" if ( a || b ) then printfn "Line 4 - Condition is true" else printfn "Line 4 - Condition is not true"
编译并执行程序时,将产生以下输出-
Line 1 - Condition is true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true
按位运算符对位进行运算并执行逐位运算。&&&的真值表(按位AND),|||(按位或)和^^^(按位异或)如下-
p | q | p &&& q | p ||| q | p ^^^ q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
假设A=60;和B=13;现在以二进制格式,它们将如下所示-
A=0011 1100 B=0000 1101 ----------------- A&&&B=0000 1100 A ||| B=0011 1101 A ^^^ B=0011 0001 ~~~ A=1100 0011
下表列出了F#语言支持的按位运算符。假设变量A=60,变量B=13,则-
Operator | Remark | Example |
---|---|---|
&&& | 按位 AND 运算符 | (A &&& B)=12, which is 0000 1100 |
||| | 按位 OR 运算符 | (A ||| B)=61, which is 0011 1101 |
^^^ | 按位 XOR 运算符 | (A ^^^ B)=49, which is 0011 0001 |
~~~ | 按位 补码 运算符 | (~~~A)=-61, which is 1100 0011 in 2's complement form. |
<<< | 按位 左移 运算符 | A <<< 2=240 which is 1111 0000 |
>>> | 按位 右移 运算符 | A >>> 2=15 which is 0000 1111 |
let a : int32 = 60 // 60=0011 1100 let b : int32 = 13 // 13=0000 1101 let mutable c : int32 = 0 c <- a &&& b // 12=0000 1100 printfn "Line 1 - Value of c is %d" c c <- a ||| b // 61=0011 1101 printfn "Line 2 - Value of c is %d" c c <- a ^^^ b // 49=0011 0001 printfn "Line 3 - Value of c is %d" c c <- ~~~a // -61=1100 0011 printfn "Line 4 - Value of c is %d" c c <- a <<< 2 // 240=1111 0000 printfn "Line 5 - Value of c is %d" c c <- a >>> 2 // 15=0000 1111 printfn "Line 6 - Value of c is %d" c
编译并执行程序时,将产生以下输出-
Line 1 - Value of c is 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is 49 Line 5 - Value of c is 240 Line 6 - Value of c is 15
下表显示了F#语言中运算符和其他表达式关键字的优先顺序,从最低优先级到最高优先级。
Operator | Associativity |
---|---|
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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕