F#運(yùn)算符

2018-12-14 18:12 更新

運(yùn)算符是一個(gè)符號(hào),通知編譯器執(zhí)行特定的數(shù)學(xué)或邏輯操作。 F#擁有豐富的內(nèi)置操作符,并提供以下類型的操作符

  • 算術(shù)運(yùn)算符
  • 比較運(yùn)算符
  • 布爾運(yùn)算符
  • 位運(yùn)算符

算術(shù)運(yùn)算符

下表列出了所有由F#語(yǔ)言支持的算術(shù)運(yùn)算符。假設(shè)變量A持有10和變量B持有20話 

運(yùn)算符描述例子
+再添兩個(gè)操作數(shù)A + B will give 30
-減去來(lái)自第一第二操作數(shù)A - B will give -10
*相乘兩個(gè)操作數(shù)A * B will give 200
/通過(guò)去分子除以分母B / A will give 2
模運(yùn)算和整數(shù)余數(shù)B % A will give 0
**指數(shù)運(yùn)算符,將運(yùn)算符提升為另一個(gè)
B**A will give 20

例子

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

當(dāng)編譯和執(zhí)行程序時(shí),它會(huì)產(chǎn)生以下輸出 

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

比較運(yùn)算符

下表列出了所有由F#語(yǔ)言支持的比較運(yùn)算符。這些二進(jìn)制比較運(yùn)算符可用于整數(shù)和浮點(diǎn)類型。這些運(yùn)算符返回布爾類型的值。

假設(shè)變量A持有10和變量B持有20,那么 

運(yùn)算符描述
=檢查,如果兩個(gè)操作數(shù)的值相等與否,如果是,則條件變?yōu)檎妗?/td>(A == B)是不正確的。
<>檢查,如果兩個(gè)操作數(shù)的值相等與否,如果值不相等,則條件變?yōu)檎妗?/td>(A <> B)是真實(shí)的。
>檢查左操作數(shù)的值大于右操作數(shù)的值,如果是,則條件為真。(A> B)是不正確的。
<檢查左操作數(shù)的值小于右操作數(shù)的值,如果是,則條件為真。(A <B)為真。
> =檢查左操作數(shù)的值大于或等于右邊的操作數(shù)的值,如果是,則條件為真。(A> = B)是不正確的。
<=檢查左操作數(shù)的值小于或等于右邊的操作數(shù)的值,如果是,則條件為真。(A <= B)為真。

例子

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"

當(dāng)編譯和執(zhí)行程序時(shí),將產(chǎn)生以下輸出
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

布爾運(yùn)算符

下表顯示了F#語(yǔ)言支持的所有布爾運(yùn)算符。 假設(shè)變量A holdstrue和變量B holdfalse,則 

運(yùn)算符描述
&&所謂布爾AND運(yùn)算。如果兩個(gè)操作數(shù)都非零,則條件變?yōu)檎妗?/td>(A && B)是假的。
||所謂OR運(yùn)算符。如果任何兩個(gè)操作數(shù)是非零,則條件變?yōu)檎妗?/td>(A || B)是真實(shí)的。
not所謂的布爾非操作。使用反轉(zhuǎn)其操作數(shù)的邏輯狀態(tài)。如果條件為真,那么邏輯非運(yùn)算符將假的。沒(méi)有(A && B)是真實(shí)的。

例子

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"

當(dāng)編譯和執(zhí)行程序時(shí),將產(chǎn)生以下輸出

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

位運(yùn)算符

按位運(yùn)算符處理位并執(zhí)行逐位操作。 &&&(按位與)的真值表,||| (按位或)和^^^(逐位異或)如下 

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

假設(shè)A = 60; 和B = 13; 現(xiàn)在在二進(jìn)制格式,如下

A = 0011 1100
B = 0000 1101
-----------------
一個(gè)&&& B = 0000 1100
一個(gè)||| B = 0011 1101
一個(gè)^^^ B = 0011 0001
~~~ A = 1100 0011

由F#語(yǔ)言支持位運(yùn)算符列于下表。假設(shè)變量A持有60和變量B持有13,那么 -

運(yùn)算符描述
&&&二進(jìn)制和運(yùn)營(yíng)商副本,如果它存在于兩個(gè)操作數(shù)了一下結(jié)果。(A &&& B)將給出12,即0000 1100
|||二進(jìn)制或操作員副本,如果它存在于一個(gè)操作數(shù)了一下。(A ||| B)將給出61,即0011 1101
^^^二進(jìn)制XOR算子拷貝如果在一個(gè)操作數(shù)設(shè)置,但不是這兩個(gè)位。(A ^^^ B)將給出49,即0011 0001
~~~二進(jìn)制的補(bǔ)運(yùn)算符是一元的,具有“翻轉(zhuǎn)”位的效果。(~~~ A)將給出-61,其是2的補(bǔ)碼形式的1100 0011。
<<<二進(jìn)制左移運(yùn)算。左邊的操作數(shù)的值被移動(dòng)通過(guò)正確的操作數(shù)指定的位數(shù)離開(kāi)了。A <<< 2將給出240,即1111 0000
>>>二進(jìn)制右移操作。左邊的操作數(shù)的值由右操作數(shù)指定的位數(shù)向右移動(dòng)。A >>> 2將給出15,即0000 1111

運(yùn)算符的優(yōu)先級(jí)

下表顯示了F#語(yǔ)言中運(yùn)算符和其他表達(dá)式關(guān)鍵字的優(yōu)先級(jí)順序,從最低優(yōu)先級(jí)到最高優(yōu)先級(jí)。

運(yùn)算符關(guān)聯(lián)性
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

當(dāng)編譯和執(zhí)行程序時(shí),將產(chǎn)生以下輸出

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

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)