Haskell Program to convert int type variables to String


In Haskell, we will convert int type variables into string by using user-defined function, intToString along with show, map and printf functions. In the first example, we are going to use (intToString n = show n) function and in the second example, we are going to use (intToString n = intercalate "" (map show [n])). And in the third example, we are going to use (intToString n = printf "%d" n).

Algorithm

  • Step 1 − The intToString function is defined using show function as, intToString n = show n.

  • Step 2 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 3 − The variable named, “n” is being initialized. It will hold the integer that is to be converted to respective string value.

  • Step 4 − The function, intToString is being called and n is passed to it.

  • Step 5 − The resultant string value is printed to the console using ‘putStrLn’ statement after the function is called.

Example 1

In this example, the show function in Haskell converts a value of any type that is an instance of the Show typeclass to a String. Since Int is an instance of the Show typeclass, the expression show n returns a String representation of the integer value n.

intToString :: Int -> String
intToString n = show n

main :: IO ()
main = do
   let n = 42
   putStrLn "String is:"
   putStrLn (intToString n)

Output

String is:
42

Example 2

In this example, the map function applies a given function to each element of a list, in this case the show function to the list [n]. The resulting list of Strings is then concatenated using intercalate, which takes a separator String and a list of Strings and inserts the separator between each adjacent pair of elements. In this case, the separator is the empty String, so the list of Strings is simply concatenated into a single String.

import Data.List (intercalate)

intToString :: Int -> String
intToString n = intercalate "" (map show [n])


main :: IO ()
main = do
   let n = 42
   putStrLn "String is:"
   putStrLn (intToString n)

Output

String is:
42

Example 3

In this example, the printf function is a powerful string formatting function that allows a wide range of formatting options. In this case, the %d format specifier is used to print an integer value, and the n variable is provided as an argument. This implementation is very concise, but can be less flexible than the other approaches if additional formatting options are needed.

import Text.Printf (printf)

intToString :: Int -> String
intToString n = printf "%d" n
main :: IO ()
main = do
   let n = 42
   putStrLn "String is:"
   putStrLn (intToString n)

Output

String is:
42

Conclusion

Integer to string conversion is the process of converting an integer value to its equivalent string representation. In programming, this operation is often required when integer values need to be displayed or manipulated as strings, such as when printing output or when concatenating strings. In Haskell, an Int variable is converted to string using user-defined intToString function along with show, map and printf functions.

Updated on: 20-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements