Haskell Program to add two numbers

Program

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

This Haskell program adds two numbers and prints the result.

  1. main = do

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

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

    • Prints a message to the console.
    • putStrLn is used to print a string followed by a newline.
  4. print (a + b)

    • Computes the sum of a and b (10 + 20 = 30).
    • print is used to display the result on the console.

Output

$ ghc add_two_numbers.hs 
[1 of 1] Compiling Main             ( add_two_numbers.hs, add_two_numbers.o )
Linking add_two_numbers ...
$ ./add_two_numbers 
Sum of two numbers is:
30