Haskell Program to initialize and print a complex number


This tutorial will help us in initializing and printing a complex number. In Haskell, the Data.Complex library provides a Complex type to represent complex numbers.

Method 1: Using Complex data type

This method defines a Complex data type that holds the real and imaginary parts of a complex number, and an instance of the Show type class for Complex, which allows it to be printed using the putStrLn function.

In the main function, it creates a complex number object with real part and imaginary part. Then it prints the complex number using the putStrLn function and the show function.

Algorithm

  • Step 1 − Complex data type is defined, that will hold the real and imaginary parts of the complex number.

  • Step 2 − Show instance is defined to represent the complex number.

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − A variable named, “c” is being initialized. It will have the real and imaginary number value that is to be represented as a complex number.

  • Step 5 − Final resultant complex number value is displayed by using ‘putStrLn’ statement.

Example

Program to initialize and print a complex number by using Complex data type.

data Complex = Complex { real :: Double, imag :: Double }
instance Show Complex where
   show (Complex r i) = (show r) ++ " + " ++ (show i) ++ "i"
main :: IO ()
main = do
   let c = Complex { real = 3.0, imag = 4.0 }
   putStrLn $ "The complex number is: " ++ (show c)

Output

The complex number is: 3.0 + 4.0i

Method 2: Using a custom data type with a constructor and type class

In this method, a custom data type Complex is defined with a constructor that takes in two double values, representing the real and imaginary parts of the complex number. The ComplexNumber type class is defined which has 3 functions real, imag and toString to access the real and imaginary parts and to convert it to string. An instance of the ComplexNumber type class is defined for the Complex data type. The putStrLn function is used to print the complex number using the toString function.

Algorithm

  • Step 1 − Custom data type, Complex is defined to hold the real and imaginary parts.

  • Step 2 − ComplexNumber type class is defined which has 3 functions real, imag and toString to access the real and imaginary parts and to convert it to string.

  • Step 3 − An instance of the ComplexNumber type class is defined for the Complex data type using above three functions.

  • Step 4 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 5 − A variable named, “c” is being initialized. It will have the real and imaginary number value that is to be represented in the form of a complex number.

  • Step 6 − Final resultant complex number value is displayed by using ‘putStrLn’ statement.

Example 1

Program to initialize and print a complex number using a custom data type with constructor and type class.

data Complex = Complex Double Double deriving Eq
class ComplexNumber a where
   real :: a -> Double
   imag :: a -> Double
   toString :: a -> String
instance ComplexNumber Complex where
   real (Complex r _) = r
   imag (Complex _ i) = i
   toString c = (show $ real c) ++ " + " ++ (show $ imag c) ++ "i"
main :: IO ()
main = do
   let c = Complex 3.0 4.0
   putStrLn $ "The complex number is: " ++ (toString c)

Output

The complex number is: 3.0 + 4.0i 

Example 2

In this example, a tuple is used to represent a complex number, with the first element being the real part and the second element being the imaginary part. The putStrLn function is used to print the complex number, which is automatically converted to a string using the show function.

type Complex = (Double, Double)
main :: IO ()
main = do
   let c = (3.0, 4.0)
   putStrLn $ "The complex number is: " ++ (show c)

Output

The complex number is: (3.0,4.0)

Conclusion

In Haskell, the number can be initialized and printed as a complex number by various methods including Complex data type, Custom data type with constructor and type class or by using tuple, etc.

In every approach, the real and imaginary parts are passed that needs to be represented in the form of complex number.

Updated on: 19-Jan-2023

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements