Haskell Program to divide two numbers

Program

main = do
 let a = 12
 let b = 4
 putStrLn "Division of two numbers"
 print (a / b)

This Haskell program performs division between two numbers and prints the result.

  1. main = do

    • Defines the main function where input and output operations occur.
  2. let a = 12 and let b = 4

    • Declares two immutable variables, a and b, with values 12 and 4, respectively.
  3. putStrLn "Division of two numbers"

    • Prints a message indicating the operation being performed.
  4. print (a / b)

    • Performs division (12 / 4).
    • / is the division operator in Haskell, used for floating-point division.
    • If a and b are both integers, Haskell will throw a type error since / works with Float or Double.
    • To fix this, a and b should be explicitly converted using fromIntegral a / fromIntegral b.

Output

$ ghc divide_two_numbers.hs 
[1 of 1] Compiling Main             ( divide_two_numbers.hs, divide_two_numbers.o )
Linking divide_two_numbers ...
$ ./divide_two_numbers 
Division of two numbers
3.0