Haskell Program to subtract two numbers

Program

main = do
 let a = 20
 let b = 10
 putStrLn "Difference of two numbers is:"
 print (a - b)

This Haskell program subtracts two numbers and prints the result.

  1. main = do

    • Defines the main function where input and output operations take place.
  2. let a = 20 and let b = 10

    • Declares two immutable variables, a and b, with values 20 and 10, respectively.
    • let is used to define variables in Haskell.
  3. putStrLn "Difference of two numbers is:"

    • Prints a message to the console.
    • putStrLn is used to display a string with a newline at the end.
  4. print (a - b)

    • Computes the difference between a and b (20 - 10 = 10).
    • print is used to display the result on the console.

Output

$ ghc subtract_two_numbers.hs 
[1 of 1] Compiling Main             ( subtract_two_numbers.hs, subtract_two_numbers.o )
Linking subtract_two_numbers ...
$ ./subtract_two_numbers 
Difference of two numbers is:
10