F#運算符

2018-12-14 18:12 更新

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

  • 算術運算符
  • 比較運算符
  • 布爾運算符
  • 位運算符

算術運算符

下表列出了所有由F#語言支持的算術運算符。假設變量A持有10和變量B持有20話 

運算符描述例子
+再添兩個操作數(shù)A + B will give 30
-減去來自第一第二操作數(shù)A - B will give -10
*相乘兩個操作數(shù)A * B will give 200
/通過去分子除以分母B / A will give 2
模運算和整數(shù)余數(shù)B % A will give 0
**指數(shù)運算符,將運算符提升為另一個
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

當編譯和執(zhí)行程序時,它會產(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

比較運算符

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

假設變量A持有10和變量B持有20,那么 

運算符描述
=檢查,如果兩個操作數(shù)的值相等與否,如果是,則條件變?yōu)檎妗?/td>(A == B)是不正確的。
<>檢查,如果兩個操作數(shù)的值相等與否,如果值不相等,則條件變?yōu)檎妗?/td>(A <> B)是真實的。
>檢查左操作數(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"

當編譯和執(zhí)行程序時,將產(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

布爾運算符

下表顯示了F#語言支持的所有布爾運算符。 假設變量A holdstrue和變量B holdfalse,則 

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

例子

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"

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

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

位運算符

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

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

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

A = 0011 1100
B = 0000 1101
-----------------
一個&&& B = 0000 1100
一個||| B = 0011 1101
一個^^^ B = 0011 0001
~~~ A = 1100 0011

由F#語言支持位運算符列于下表。假設變量A持有60和變量B持有13,那么 -

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

運算符的優(yōu)先級

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

運算符關聯(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

當編譯和執(zhí)行程序時,將產(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)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號