Haskell程式新增兩個複數
本教程將討論編寫一個程式,在Haskell程式語言中新增兩個複數。Haskell是一種宣告式、強型別和函數語言程式設計語言。Haskell中的計算是數學函式。
複數是實數和虛數值的組合。
例如:1 + 2i,其中i是√-1
在本教程中,我們將看到四種新增兩個複數的方法。
使用加法中綴運算子“+”計算加法。
在單獨的函式中實現加法計算。
使用中綴運算子“+”作為函式計算加法。
透過提取實部和虛部來計算複數的加法。
語法
遵循以下語法來新增兩個複數
c1 + c2
其中c1和c2是複數,而“+”是加法的中綴運算子。
要在Haskell中定義複數,我們需要使用語法Data.Complex從包Data匯入模組Complex,這對於支援複數是必要的。我們可以使用語法(a :+ b)構造複數,其中a和b是數字。(a :+ b)與(a + ib)相同,“:+”是構造複數的建構函式。
演算法步驟
初始化複數。
實現加法邏輯。
列印輸出。
使用中綴運算子“+”新增複數
示例
使用中綴運算子“+”新增複數的程式
import Data.Complex main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 3 -- computing addition using infix operator let c = a + b print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)
輸出
"Complex number 1 = " 1.0 :+ 2.0 "Complex number 2 = " 2.0 :+ 3.0 Addition of Complex numbers a and b is: 3.0 :+ 5.0
在上面的程式中,我們從包Data匯入了模組Complex。在main函式中,我們使用複數建構函式“:+”初始化了兩個變數a和b,並用複數賦值。我們使用加法中綴運算子“+”將這兩個數字相加,並將值載入到變數c中。最後,我們使用函式print列印結果複數。
使用單獨的函式新增複數
示例
使用單獨的函式新增複數的程式
import Data.Complex -- function definition for adding complex numbers addComplex a b = a + b main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 7 print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- computing addition by invoking the addComplex funtion let c = addComplex a b -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)
輸出
"Complex number 1 = " 1.0 :+ 2.0 "Complex number 2 = " 2.0 :+ 7.0 Addition of Complex numbers a and b is: 3.0 :+ 9.0
在上面的程式中,我們從包Data匯入了模組Complex。我們定義了一個函式addComplex,它以兩個複數作為輸入,並返回這兩個數字的和。在main函式中,我們使用複數建構函式“:+”初始化了兩個變數a和b,並用複數賦值。我們使用a和b作為引數呼叫了函式addComplex,並將返回值載入到變數c中。最後,我們使用函式print列印結果複數。
使用中綴運算子作為函式新增複數。
示例
使用中綴運算子作為函式新增複數的程式。
import Data.Complex main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 10 print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- computing addition using infix operator as function let c = (+) a b -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)
輸出
"Complex number 1 = " 1.0 :+ 2.0 "Complex number 2 = " 2.0 :+ 10.0 Addition of Complex numbers a and b is: 3.0 :+ 12.0
在上面的程式中,我們從包Data匯入了模組Complex。在main函式中,我們使用複數建構函式“:+”初始化了兩個變數a和b,並用複數賦值。我們使用中綴運算子作為函式(+),並以a和b作為引數,將這兩個數字相加,並將值載入到變數c中。正如所說,Haskell是一種函數語言程式設計語言,所有運算子都是函式的語法糖。我們可以透過將它們封裝在括號中將中綴運算子轉換為函式。
示例 - 加法運算子“+”可以使用語法(+)轉換為函式。最後,我們使用函式print列印結果複數。
透過提取實部和虛部新增複數
示例
透過提取實部和虛部計算複數加法的程式。
import Data.Complex main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 3 print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- computing addition using infix operator as function let realSum = (realPart a) + (realPart b) let imagSum = (imagPart a) + (imagPart b) let c = realSum :+ imagSum -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)
輸出
"Complex number 1 = " 1 :+ 2 "Complex number 2 = " 2 :+ 3 Addition of Complex numbers a and b is: 3 :+ 5
在上面的程式中,我們從包Data匯入了模組Complex。在main函式中,我們使用複數建構函式“:+”初始化了兩個變數a和b,並用複數賦值。我們使用函式realPart獲取複數的實部,該函式以複數作為輸入並返回複數的實部,並將它們載入到變數realSum中。接下來,我們使用函式imagPart獲取複數的虛部,該函式以複數作為輸入並返回複數的虛部,並將它們載入到變數imagSum中。我們使用建構函式“:+”從變數realSum和imagSum構造了一個複數,並將其載入到變數c中。最後,我們列印結果複數。
結論
在本教程中,我們討論了四種在Haskell程式語言中編寫程式來新增複數的不同方法。