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.
-
main = do- Defines the main function where input and output operations occur.
-
let a = 12andlet b = 4- Declares two immutable variables,
aandb, with values12and4, respectively.
- Declares two immutable variables,
-
putStrLn "Division of two numbers"- Prints a message indicating the operation being performed.
-
print (a / b)- Performs division (
12 / 4). /is the division operator in Haskell, used for floating-point division.- If
aandbare both integers, Haskell will throw a type error since/works withFloatorDouble. - To fix this,
aandbshould be explicitly converted usingfromIntegral a / fromIntegral b.
- Performs division (
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