Monday, December 17, 2018

Haskell tutorials - Hello World Example Program

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

Haskell Hello World Program

--HelloWorld.hs
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.

How Haskell "Hello, World!" Program Works?

  • -- Helloworld.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.
  • module Main where
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 
  • main = putStrLn "Hello, World"
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

Compile and execute the haskell program code

Haskell code can be executed in two ways
  • Using GHC interactive shell
  • ghc compiler and executable files
Execute the Haskell Programs using GHC

To compile the Haskell code
ghc HelloWorld.hs
[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 

./HelloWorld 
Hello, World

For Windows Users
HelloWorld.exe
Hello, World

The above steps (Compilation and execution) can be replaced with a runhaskell command

runhaskell HelloWorld.hs
Hello, World

Learning things


  • Haskell programs can be compiled and run in executable form or run interactively
  • Function declaration not required types can be defined anywhere
  • The syntax is easy, simple and clean, function calls just required function name followed by space and arguments. Syntax in the function sand code doest not required {} or ()


Related article


EmoticonEmoticon