Updated April 7, 2023
Introduction to Haskell do notation.
If you want to handle the input and output operations in Haskell, then we can use do notation for that. Do notations are simply designed to handle the IO operations in Haskell. To represent the do notation, we can simply use the ‘do’ keyword; after this, we can write our code that we want to execute. In most common general coding practice for Haskell, it usually comes up with the main module. This main module should always be followed by the do notation or any other logic, if any. In the coming section of the tutorial, we will see the do notation internal working in more detail to get a better understanding of it. Also, we will see its usage and implementation as well for beginners to understand it better.
Syntax
Let’s talk about its syntax in detail; we have so many ways defined in Haskell to use this notation; as already discussed, it is used for input-output operation, so let’s one syntax to handle this using do notation in Haskell, see below;
1. main = do: This one is the basic use of do notation in Haskell; in the coming section of the tutorial, we will discuss this in detail.
2. do {operation 1; operation 2; operation 3; and so on ..}: This is the other way to use the do notation with the {} curly braces in Haskell. We will also discuss this in detail in the coming section of the tutorial.
Let’s take a practice syntax for beginners to understand it better see below;
e.g. :
main = do
// your logic goes here..
As you can see, it is very easy to use in Haskell to execute the output and input operation without any hurdle. In the coming section of the do notation, we will see some details about it. Also, working example using do notation in Haskell for beginners to understand it better and start using while coding in Haskell.
How to use do notation in Haskell?
As we already know that now, do notation in Haskell is very easy to use and implement. These are Haskell’s in-built function, so we can use them directly without any dependency or installing any library for this. Basically, as we discussed, this do notation is used to handle the input and output operation in Haskell; we also have some more ways which provide us to handle these operations, but out of which do notation is very easy to use and implement in Haskell. In this section, we will first discuss in detail about the different ways to use do notation in Haskell with their respective example for each. Let’s get started to see below;
1. do {operation 1; operation 2; operation 3; and so on ..}:
This syntax for do notation is very easy to use. Here we are using ‘{}’ curly braces to implement and handle the IO operations in Haskell using do notation. First, we have used the ‘do’ keyword, followed by the {} curly braces. And inside these braces, we can write our logic or code that we want to execute. We can write any number of input-output operations inside it but remember it should be separated by the ‘;’ semicolon that we have introduced. Let’s take a sample example for using Haskell for beginners, which will help them understand it better.
e.g. :
do { putStr "abc to test" ; putStr " i am do block" ; putStr "statement 2" ; putStr " statement 3"}
So we have written so many operations inside the curly braces using do notation in Haskell; this will execute the operations one by one. At the end of the output, we can see the lines of string as the message in the console. As you saw, it is very easy to use and execute. We have also not used or import any package to use this. We can simply run this in the Haskell editor.
2. main = do:
This is another way to use do in Haskell. In this approach, we are using the main module associated with the do notation, which means it will handle our code that we are going to write inside the do block we can say. Also, after this do block, we can write the code as the way we want, like any other normal code in Haskell. We have to just use the main module followed by the do notation immediately after that. Let’s take a closer look at the simple piece of code written in Haskell using the do with the main module for beginners to understand it better. See below;
e.g. :
main = do
print("Hello I am using main do ")
As you can see in the above line of code, we are using the main module with do notation in Haskell; it is easy to use and execute as well. We have just written the code or our logic after they do block, so it will be executed one by one as they have written.
Example of Haskell do notation.
In the below example, we are trying to use the do notation in Haskell. This is a sample example for beginners to start using it while coding in Haskell. Also, we have just showcased how we can define and use this while coding in Haskell; we have not used any import statement as well to use this.
Code:
main = do
print("Demo to show list in Haskell !!")
let list1 = [100, 50, 235, 167, 345, 909, 675, 20]
let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let list3 = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
let list4 = [5, 10, 15, 20, 15, 30, 35, 40]
let list5 = [123.4, 567.9, 56.0, 3.9, 76.9]
print("printing list element !!")
print("list one is ::" , list1)
print("list two is ::" , list2)
print("list three is ::" , list3)
print("list four is ::" , list4)
print("list five is ::" , list5)
Output:
Conclusion
As you can see, we have to use do notation in the Haskell programming if we want to execute the input and output operations. It is very easy to use and handle, readable by the developer as well. We do not require to include or import any packages to use this while programming in Haskell.
Recommended Articles
We hope that this EDUCBA information on “Haskell do notation” was beneficial to you. You can view EDUCBA’s recommended articles for more information.