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.
-
main = do
- Defines the main function where input and output operations occur.
-
let a = 10
andlet b = 20
- Declares two immutable variables,
a
andb
, with values10
and20
, respectively. let
is used to define variables in Haskell.
- Declares two immutable variables,
-
putStrLn "Sum of two numbers is:"
- Prints a message to the console.
putStrLn
is used to print a string followed by a newline.
-
print (a + b)
- Computes the sum of
a
andb
(10 + 20 = 30
). print
is used to display the result on the console.
- Computes the sum of
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