How to print in Haskell?

Alex Rivers

Haskell is a popularly used modern programming language with higher programmer productivity. It is purely a functional programming language, which means it has all mathematical functions. It is a standard high-level programming language with an expressive syntax and advanced built-in architecture. It is specially designed for large-scale industrial production, written in mathematical form.

How to write code in Haskell?

To write a code in Haskell, all we need is a text editor and a compiler, and for this article, we will be using GHC (Glasgow Haskell Compiler), which can be downloaded from the official site Haskell.

We write our Haskell code in the editor and save the file with a .hs extension. GHC reads and compiles the scripts. It has an interactive mode that can use the functions from the scripts loaded in GHC to produce an output. It is swift and easy to do so rather than compiling the program after every small change.

What is Interactive Mode?

Let’s say you have created a script called program.hs, and you have defined some
functions in it, so the interactive mode is activated by typing ‘ghci’ in your command
prompt. Ensure you have invoked ghci in the same directory in which program.hs is
stored
. The functions present in the program.hs can be called by typing:l program.

Let’s start Coding.

haskell

To start, we need to open the terminal (or command prompt or VS Code) and type ‘ghci’. You will be greeted with a message showing the version and some information about GHCI. If you want to show some message on the prompt, you have to type the message in string format, which is generally written like this “_”.

For example, if we want to show a message saying ‘This is my first program’, we need to type “”. This is my first program”” and enter. That’s it, and you will get a message on the screen with the exact text. Here is how the output will look like:

This is my first program

You can also add two or more strings to create a sentence. For example, adding the string “Hello” and string “World” will result in “Hello World”, which is done by typing “Hello” + “World” and pressing enter, the output will be “Hello World”:

Hello World

Using putStrLn to print a message

Let’s check how to use the syntax ‘putStrLn’ to print a message:

  • To use this first, we will define main as an input-output function which is done by typing :
main:: IO () 
  • After that, we print the message Hello World as follows:
main = putStrLn “ Hello World”

Output:

Hello World

Working:

Here, we defined the main function, and inside the main function, we call a function putStrLn with the parameters Hello and World. Here, the main function is performing the I/O action.

Performing Input and Output function

To combine several input-output actions, we use the do function. Let’s take an example and see how it works:


main = do

putStrLn ""Hello, how are you?""

mood  <- getLine

putStrLn(“ I am ” ++ mood) 

In the output, you have to enter your mood:

Hello, how are you?

I am good.

Here, the input added is ‘good’.

Working:

Here, each step is an input-output step, and the syntax has combined them. We have used the command mood <- getLine, it gets an input from the user and stores it in the variable mood, which is a string. <– getLine command is an input-output action that gives a result with the type String.

So, <- getLine takes the input from the user and stores the value in the variable mood. With this information, we can write so many examples and explore furthermore.

Conclusion

So, this is how you can print your program in Haskell, by using putStrLn or even print can do the job. This shall take your string and print it on the screen. Thus, your required outcome will be printed. getLine helps in reading the line from an input device.

And the job is done! Happy Coding!

Leave a Comment