This blog post covers learning how to write Hello world Example program with an explanation in Haskell language.
To learn any new programming language, Hello world is a sample program to learn and understand the basic program components. hello World is a First Sample program which prints Hello World to the console
Before you start writing the hello world program, Please make sure Haskell Platform installation on your machine. Haskell Platform contains GHC - Glasgow Haskell Compiler and Cabal (Common Architecture for Building Applications and Libraries).This post will not cover the Haskell platform installation
module Main where
-- hello world Sample Program
main = putStrLn "Hello, World"
The above sample program can be copied to HelloWorld.hs The Hello world program in haskell is simple to learn and easy to understand.
Haskell files are of extension ie hs.
This line is a comment which can be ignored by a compiler. Comments always start with a double hyphen and followed by space in Haskell. Comments are useful for a developer to describe the line of code.
This tells the compiler to load the Main module during execution of the program. Module in Haskell are collections of similar functions and types.
Main Module contains the main functions. We will discuss the main function in the next section. This line is optional
This line is the main function which is the entry point of the haskell program. This will be called first when the program Starts its execution.
This is the main function which contains putStrLn and String “Hello World”. putStrLn is an inbuilt function which accepts String as an argument.
Unline other programming languages, calling Functions or methods are like function(parameters). In Haskell Calling a function involves function name followed by a space and an argument
putStrLn “Hello, World” prints a String to the console
Haskell code can be executed in two ways
Execute the Haskell Programs using GHC
To compile the Haskell code
[1 of 1] Compiling Main ( HelloWorld.hs, HelloWorld.o )
Linking HelloWorld ...
ghc Glasgow Haskell Compiler is a compiler or interpreter that comes as part of Haskell platform installation This compile the Haskell code outputs generates native machine code in the form of executable file i.e “HelloWorld” file Executable file names can be customized by giving -O option
ghc -o hello HelloWorld.hs
This option -o tells the compiler to name the executable as hello The executable file can be run using below approaches. Please issue the following code at the command line
For Unix Flavours
For Windows Users
The above steps (Compilation and execution) can be replaced with a runhaskell command