Haskell Program to create Case statement


In Haskell, we can use pattern matching, digitToInt, reads and a user-defined functions to convert char-type variables to int. In the first example, we are going to use cases for matching the pattern and in the second example, we are going to use (charToInt c = if isDigit c then Just (digitToInt c) else Nothing). And in third example, we are going to use (charToInt c = case reads [c] of [(x, "")] -> Just x and _ -> Nothing) function.

Algorithm

  • Step 1 − The internal function is defined

  • 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.

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

  • Step 4 − The cases are defined for comparing the digits.

  • Step 5 − The resultant Integer 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 charToInt function with the help of fromEnum function to convert the char type variables to Int.

charToInt :: Char -> Int
charToInt c = fromEnum c - fromEnum '0'

main :: IO ()
main = do
   let digit = '4'
   case digit of
      '0' -> putStrLn "The integer value is 0."
      '1' -> putStrLn "The integer value is 1."
      '2' -> putStrLn "The integer value is 2."
      '3' -> putStrLn "The integer value is 3."
      '4' -> putStrLn "The integer value is 4."
      '5' -> putStrLn "The integer value is 5."
      '6' -> putStrLn "The integer value is 6."
      '7' -> putStrLn "The integer value is 7."
      '8' -> putStrLn "The integer value is 8."
      '9' -> putStrLn "The integer value is 9."
      _   -> putStrLn "Invalid input. Please enter a digit character."

Output

The integer value is 4.

Example 2

In this example, the function is defined using user-defined charToInt function with the help of digitToInt and isDigit function to convert the char type variables to Int.

import Data.Char (digitToInt, isDigit)

charToInt :: Char -> Maybe Int
charToInt c = if isDigit c then Just (digitToInt c) else Nothing

main :: IO ()
main = do
   let digit = '6'
   case charToInt digit of
      Just x  -> putStrLn $ "The integer value is " ++ show x ++ "."
      Nothing -> putStrLn "Invalid input. Please enter a digit character."

Output

The integer value is 6.

Example 3

In this example, the function is defined using user-defined charToInt function with the help of reads function to convert the char type variables to Int.

import Data.Char (digitToInt)

charToInt :: Char -> Maybe Int
charToInt c = case reads [c] of
               [(x, "")] -> Just x
               _   -> Nothing

main :: IO ()
main = do
   let digit = '5'
   case charToInt digit of
      Just x  -> putStrLn $ "The integer value is " ++ show x ++ "."
      Nothing -> putStrLn "Invalid input. Please enter a digit character."

Output

The integer value is 5.

Conclusion

The conversion of a char variable to an int variable typically involves extracting the integer value of the character from its character encoding. This can be done using built-in functions provided by the programming language, or by implementing a custom function to extract the integer value from the character encoding. In Haskell, a char type variables is converted to int using user-defined function along with pattern matching, digitToInt and reads functions.

Updated on: 13-Mar-2023

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements