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 = 10andlet b = 20- Declares two immutable variables,
aandb, with values10and20, respectively. letis used to define variables in Haskell.
- Declares two immutable variables,
-
putStrLn "Sum of two numbers is:"- Prints a message to the console.
putStrLnis used to print a string followed by a newline.
-
print (a + b)- Computes the sum of
aandb(10 + 20 = 30). printis 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