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编程语言中编写程序来添加复数的不同方法。

更新于: 2022年10月27日

716 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.