Haskell tutorials - Hello World Example Program

This blog post covers learning how to write the 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 the First Sample program that prints Hello World to the console

Before you start writing the hello world program, Please make sure the Haskell Platform installation is 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.

How to write a Hello World Program in Haskell

  • Open any text editor
  • Create a new text file.
  • Add the below sample code to the file
  • Save the file as HelloWorld.hs
--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 are followed by a 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 the execution of the program. Modules 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. It calls first when the program starts its execution.

This is the main function that contains putStrLn and String “Hello World”. putStrLn is an inbuilt function that accepts String as an argument.

Unlike other programming languages, calling Functions or methods are like functions (parameters). In Haskell Calling a function involves a function name followed by a space and an argument.

putStrLn “Hello, World” prints a String to the console

How to 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 the Haskell platform installation
This compilation of the Haskell code outputs generates native machine code in the form of an 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 the 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 require function names followed by space and arguments. Syntax in the function and code doest not required {} or ()