Haskell Program to convert int type variables to char


In Haskell, we will convert int type variables to char by using user-defined function using chr function, toEnum function and list indexing. In the first example, we are going to use (intToChar I | i >= 0 && i <= 255 = Just (chr i) | otherwise = Nothing) and in the second example, we are going to use (intToChar I | i >= 0 && i <= 255 = Just (toEnum i :: Char) | otherwise = Nothing). And in third example, we are going to use (intToChar I | i >= 0 && i <= 255 = Just (['\NUL'..] !! i) | otherwise = Nothing) function.

Algorithm

  • Step 1 − The intToChar function is defined using chr function as,

intToChar i
   | i >= 0 && i <= 255 = Just (chr i)
   | otherwise          = Nothing.
  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the intToChar function is called with the argument and the result is printed to the console.

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

  • Step 4 − The cases are defined.

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

Example 1

In this example, the function is defined using user-defined intToChar function with the help of chr function to convert the Int type variables to Char.

import Data.Char (chr)

intToChar :: Int -> Maybe Char
intToChar i
   | i >= 0 && i <= 255 = Just (chr i)
   | otherwise          = Nothing

main :: IO ()
main = do
   let num = 97
   case intToChar num of
      Just c  -> putStrLn $ "The character value is " ++ [c] ++ "."
      Nothing -> putStrLn "Invalid input. Please enter an integer between 0 and 255."

Output

The character value is a.

Example 2

In this example, the function is defined using user-defined intToChar function with the help of toEnum function to convert the Int type variables to Char.

intToChar :: Int -> Maybe Char
intToChar i
   | i >= 0 && i <= 255 = Just (toEnum i :: Char)
   | otherwise          = Nothing

main :: IO ()
main = do
   let num = 97
   case intToChar num of
      Just c  -> putStrLn $ "The character value is " ++ [c] ++ "."
      Nothing -> putStrLn "Invalid input. Please enter an integer between 0 and 255."

Output

The character value is a.

Example 3

In this example, the function is defined using user-defined intToChar function with the help of list indexing to convert the Int type variables to Char.

intToChar :: Int -> Maybe Char
intToChar i
   | i >= 0 && i <= 255 = Just (['\NUL'..] !! i)
   | otherwise          = Nothing

main :: IO ()
main = do
   let num = 97
   case intToChar num of
      Just c  -> putStrLn $ "The character value is " ++ [c] ++ "."
      Nothing -> putStrLn "Invalid input. Please enter an integer between 0 and 255."

Output

The character value is a.

Conclusion

In Haskell, an Int type variable can be converted to a Char type variable by creating a Char with the corresponding Unicode code point value. A Unicode code point is an integer value that represents a specific character in the Unicode character set. In Haskell, a int type variables is converted to char using user-defined function along with chr and toEnum function and also by using list indexing.

Updated on: 13-Mar-2023

884 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements