- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to get the real part from a Complex number
In Haskell, we can use realPart function and pattern matching to get the real part from a complex number. In the first example we are going to use (realPart c) function and in the second example, we are going to use patter matching as (x:+_) = c.
Algorithm
Step 1 − The Data.Complex module is imported to work over the complex numbers.
Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 3 − The variable named, “c” is being initialized. It will hold the complex number value whose real part is to be printed.
Step 4 − The resultant real part is printed to the console on calling the realPart function.
Example 1
In this example, we are going to see that how we can print the real part of the complex number. This can be done by using realPart function.
import Data.Complex main :: IO () main = do let c = 3 :+ 4 print (realPart c)
Output
3
Example 2
In this example, the complex number c is matched against the pattern (x:+_), where x is the real part and _ is a placeholder for the imaginary part. This extracts the real part of the complex number and assigns it to x.
import Data.Complex main :: IO () main = do let c = 3 :+ 4 let (x:+_) = c print x
Output
3
Conclusion
The real part of a complex number is the coefficient of the real component in its standard form of representation, a + bi, where a and b are real numbers, and i is the imaginary unit. In Haskell, a real part of the Complex number can be fetched, by using realPart function, or by using pattern matching.