How to Write A Simple "Hello World" Program In Haskell?

10 minutes read

To write a simple "Hello World" program in Haskell, follow these steps:

  1. Open a text editor or an Integrated Development Environment (IDE) that supports Haskell programming.
  2. Create a new Haskell source file with a .hs extension, such as helloWorld.hs.
  3. In the newly created file, start by defining a main function. The main function is the entry point for any Haskell program.
  4. Inside the main function, use the putStrLn function to print the string "Hello, World!" to the console. The putStrLn function takes a string as an argument and displays it followed by a newline character. So, your code should look like this:
1
2
main :: IO ()
main = putStrLn "Hello, World!"


  1. Save the file.
  2. Now, open a terminal or command prompt and navigate to the directory where you saved the helloWorld.hs file.
  3. Compile the Haskell source file into an executable by running the following command: ghc helloWorld.hs This command uses the GHC (Glasgow Haskell Compiler) to compile the code.
  4. After the compilation process finishes successfully, an executable file named helloWorld (or helloWorld.exe on Windows) will be generated in the same directory.
  5. Run the generated executable by executing the following command: ./helloWorld On Windows, the command would be helloWorld.exe.
  6. The program will be executed, and you should see the output "Hello, World!" printed in the console.


Congratulations! You have successfully written and executed a simple "Hello World" program in Haskell.

Best Haskell Books to Read in 2024

1
Get Programming with Haskell

Rating is 5 out of 5

Get Programming with Haskell

2
Haskell in Depth

Rating is 4.9 out of 5

Haskell in Depth

3
Programming in Haskell

Rating is 4.8 out of 5

Programming in Haskell

4
Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

Rating is 4.7 out of 5

Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

5
Programming in Haskell

Rating is 4.6 out of 5

Programming in Haskell

6
Haskell from the Very Beginning

Rating is 4.5 out of 5

Haskell from the Very Beginning

7
Developing Web Apps with Haskell and Yesod: Safety-Driven Web Development

Rating is 4.4 out of 5

Developing Web Apps with Haskell and Yesod: Safety-Driven Web Development

8
Real World Haskell: Code You Can Believe In

Rating is 4.3 out of 5

Real World Haskell: Code You Can Believe In

9
Haskell: The Craft of Functional Programming (International Computer Science Series)

Rating is 4.2 out of 5

Haskell: The Craft of Functional Programming (International Computer Science Series)

10
Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

Rating is 4.1 out of 5

Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming


What is type inference in Haskell?

Type inference is a feature of Haskell that allows the compiler to automatically deduce the types of expressions in a program without requiring explicit type annotations. This means that developers do not have to explicitly specify the types of every expression, and the compiler can infer them based on the context and usage of the expression.


Type inference is facilitated by Haskell's strong static typing system and its use of Hindley-Milner type inference algorithm. The compiler examines the structure of the program and applies a set of rules to infer the types of variables, functions, and expressions.


For example, consider the following code snippet in Haskell:

1
2
3
4
5
6
7
double :: Int -> Int
double x = x * 2

main :: IO ()
main = do
  let x = double 5
  print x


In this example, the type of the double function is inferred to be Int -> Int, meaning it takes an Int as an argument and returns an Int. The main function has a type signature IO (), indicating that it performs some I/O operations and returns nothing.


By inferring the types of expressions, Haskell provides strong type safety and helps prevent many common programming errors. It also enables more concise code, as there is no need for explicit type annotations in most cases.


How to define a new operator in Haskell?

In Haskell, it is not possible to directly define new operators. However, Haskell allows you to define functions with symbolic (non-alphanumeric) names, which can serve as operators.


To define a new operator-like function, you need to enclose its name in backticks (`). This allows you to use the function as an infix operator when writing code. Here's an example of how to define a new operator-like function:

1
2
3
infixl 6 <+>
(<+>) :: Int -> Int -> Int
x <+> y = x + 2 * y


In the above code:

  • infixl 6 <+> declares <+> as a left-associative infix operator with precedence level 6. You can adjust the precedence level (1-9) to match your needs.
  • (<+>) is the name of the operator-like function.
  • The type signature :: Int -> Int -> Int specifies that <+> takes two Int parameters and returns an Int.
  • The definition x <+> y = x + 2 * y defines the behavior of <+>, which in this case adds the first argument to twice the second argument.


Now you can use <+> as an infix operator:

1
2
3
main :: IO ()
main = do
    putStrLn $ show (5 <+> 3) -- prints "11"


Here, 5 <+> 3 returns the result of 5 + 2 * 3, which is 11.


What is the main function in Haskell?

In Haskell, the main function is the entry point for the program. It is defined as a top-level value and has the type IO (). The main function is executed when the program starts and is responsible for performing any necessary I/O operations or calling other functions to achieve the desired program behavior.


How to define a list in Haskell?

To define a list in Haskell, you can use the square brackets notation with items separated by commas. The elements in a list must have the same type. Here are a few examples of defining lists in Haskell:

  1. List of numbers:
1
numbers = [1, 2, 3, 4, 5]


  1. List of characters:
1
characters = ['a', 'b', 'c', 'd']


  1. List of strings:
1
strings = ["Hello", "World"]


  1. List of booleans:
1
booleans = [True, False, True]


You can also define an empty list using the square brackets without any elements:

1
emptyList = []


Lists in Haskell are immutable, meaning once defined, you cannot change or modify their contents.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To write and run a simple &#34;Hello World&#34; program in Golang, follow these steps:Open a text editor or an integrated development environment (IDE) of your choice.Create a new file and save it with a &#34;.go&#34; extension, such as &#34;hello.go&#34;.Star...
To find the memory usage of a Haskell program, you can use the GHC (Glasgow Haskell Compiler) built-in profiling options. Profiling allows you to gather information about various aspects of your program&#39;s execution, including memory usage.Here is an approa...
To call Haskell functions from Java, you can make use of the Java Native Interface (JNI). Here is a step-by-step explanation of how to do it:Write your Haskell code: Start by writing the Haskell functions you want to call from Java. The functions should be par...