F# - 运算符

F# - 运算符 首页 / F#入门教程 / F# - 运算符

运算符是一个符号,告诉编译器执行特定的数学或逻辑操作, 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,则-

OperatorRemarkExample
==相等(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,然后-

OperatorRemarkExample
&&布尔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),|||(按位或)和^^^(按位异或)如下-

pqp &&& qp ||| qp ^^^ q
00000
01011
11110
10011

假设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,则-

OperatorRemarkExample
&&&按位 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#语言中运算符和其他表达式关键字的优先顺序,从最低优先级到最高优先级。

OperatorAssociativity
asRight
whenRight
| (pipe)Left
;Right
letNon associative
function, fun, match, tryNon associative
ifNon associative
Right
:=Right
,Non associative
or, ||Left
&, &&Left
< op, >op, =, |op, &opLeft
&&& , |||, ^^^, ~~~, <<<, >>>Left
^ opRight
::Right
:?>, :?Non associative
- op, +op, (binary)Left
* op, /op, %opLeft
** opRight
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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

白话法律42讲 -〔周甲徳〕

玩转Spring全家桶 -〔丁雪丰〕

罗剑锋的C++实战笔记 -〔罗剑锋〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

如何成为学习高手 -〔高冷冷〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

结构写作力 -〔李忠秋〕

PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕

给程序员的写作课 -〔高磊〕

好记忆不如烂笔头。留下您的足迹吧 :)