Haskell tutorials - Hello World Example Program

This blog post covers learning how to write the Hello World example program with an explanation in the Haskell language.

To learn any new programming language, the Hello World program is a sample program that helps you understand the basic components of a program. Hello World is typically the first sample program that prints “Hello, World!” to the console.

Before you start writing the Hello World program, please ensure that the Haskell Platform is installed 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 have the extension .hs.

The above sample program can be compiled and executed using the GHC compiler.

How Haskell “Hello, World!” Program Works?

  • — Helloworld.hs

This line is a comment, which can be ignored by the compiler. Comments in Haskell always start with a double hyphen and are followed by a space. They are useful for developers to describe lines of code.

  • module Main where This instructs the compiler to load the Main module during the execution of the program. Modules in Haskell are collections of similar functions and types.

The Main module typically contains the main functions. We will delve into the main function in the next section. This line is optional.

  • main = putStrLn “Hello, World”

This line represents the main function, which serves as the entry point of the Haskell program. It is the first function called when the program begins execution.

The main function contains putStrLn and the String Hello World. putStrLn is an inbuilt function that takes a String as an argument.

In Haskell, calling functions is different from other programming languages. It involves stating the 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 the GHC interactive shell
  • Compiling with the GHC compiler and generating 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 included as part of the Haskell platform installation.

This compiler, when used to compile Haskell code, produces native machine code in the form of an executable file, typically named HelloWorld. Executable file names can be customized by providing the -o option.

ghc -o hello HelloWorld.hs

The -o option instructs the compiler to name the executable as “hello”. The resulting executable file can be run using the following approaches.

Please execute the following command in 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

Summary

  • Haskell programs can be compiled and run in executable form or executed interactively.
  • Function declarations are not required, and types can be defined anywhere.
  • The syntax is easy, simple, and clean; function calls just require function names followed by space and arguments. Haskell syntax does not require {} or () for functions and code blocks.