Haskell program to print a string

HaskellServer Side ProgrammingProgramming

This tutorial will discuss writing a print string in Haskell Programming Language. The Computations in Haskell are done using mathematical functions. In this tutorial, We will discuss different ways to print a String in Haskell.

  • Printing a String with the “print” function.

  • Printing a String with the “putStr” function.

  • Printing a String with the “putStrLn” function.

  • Printing multiple String concatenated.

  • Printing Strings concatenated with other data types.

Prerequisite

Strings are a series of characters that may or not have a meaningful script. Example “hello”, and “hello world” are Strings as they are series/combinations of characters.

Syntax

Following is the syntax structure to print a String.

We start with string declaration followed by String initialization and finally printing String.

String declaration and initialization outside functions and in functions using non-IO operations (eg print, getLine, putStr, etc)

variable_name :: String (This step is optional)
Example: name :: String

The above line declares the variable ‘name’ as String and the data type is bound to the variable and trying to redeclare and initialize with other the other data type throws an error.

variable_name = value
Example: name = “bob”

The above line initializes the variable ‘name’ with the value “bob” any attempt to initialize with the other data type results in a compilation error.

String declaration in functions using IO operations (eg print, getLine, putStr, etc)

let variable_name = value
Example: let name = “bob”

The above line declare and initialize variable ‘name’ with string value “bob”.

Note − With let keyword variable must be initialized when declared. The data type will be bound to the variable when the value is loaded.

Algorithm steps

  • variable declaration (optional)

  • variable initialization

  • printing variable

Example 1

Program to print String with “print” function

main :: IO () main = do -- declaring variable name using let keyword and initializing it with value bob let name = "bob" -- printing variable name print "The value of the String is:" print name

Output

The value of the String is:
bob

Note − lines starting with “--” are single line which is ignored by the compiler during program execution.

In the above program we declared a variable with name name using the let keyword because as we discussed we should declare a variable with let keyword in function using IO operations (here print is an IO operation which output value on output screen) and we initialized with value bob and printed with a print function. print is a function that prints output values on the output screen. print function adds a new line at the end of the printed value.

Example 2

Program to print String with “putStr” function

main :: IO () main = do -- declaring variable name using let keyword and initializing it with value bob let name = "bob" -- printing variable name putStr "The value of the String is:" putStr name

Output

The value of the String is: bob

In the above program we declared and initialized the variable name with value “bob” and printed using putStr function. putStr function prints the value to the output screen. putStr function doesn’t add a new line and the end of the printed value.

Example 3

Program to print String with “putStrLn” function

main :: IO () main = do -- declaring variable name using let keyword and initializing it with value bob let name = "bob" -- printing variable name putStrLn "The value of the String is:" putStrLn name

Output

The value of the String is: bob

In the above program we declared and initialized the variable name with value “bob” and printed using putStrLn function. putStrLn function prints the value to the output screen. putStrLn function adds a new line and the end of the printed value.

Example 4

Program to print multiple String concatenated.

main :: IO () main = do -- declaring variable name using let keyword and initializing it with value bob let name = "bob" let lastname = " addison" -- printing variable name concatenated with variable lastname putStrLn "The value of the concatenated String is:" putStrLn (name ++ lastname)

Output

The value of the concatenated String is:
bob addison

In the above program we declared two variable name and lastname and initialized with values “bob” and “ addison”. We printed the string using putStrLn function. The operator “++” is used to concatenate multiple Strings. Brackets should be used when printing multiple variables because of function putStrln takes only one argument it throws an error if multiple arguments are passed so, the total string should be encapsulated with brackets making it a single argument.

Example 5

Program to print Strings concatenated with other data types.

main :: IO () main = do -- declaring variable name using let keyword and initializing it with value bob let name = "bob" let grade = 8.2 -- printing string concatenated with float data type putStrLn (name ++ "'s grade is " ++ show grade)

Output

bob’s grade is 8.2

In the above program we declared two variables name and grade and initialized with values “bob” and 8.2. The function show takes other data types as an argument and returns them in String form. And we concatenated the variable name and string returned by the show function and printed them using putStrLn function.

Conclusion

In this tutorial, we discussed different ways to print strings in Haskell programming language using different functions like print, putStr, and how to print concatenated strings and strings with other data types.

raja
Updated on 11-Oct-2022 11:24:22

Advertisements