Haskell Program to Check if a String is Numeric


In Haskell we can use read functions and Data.char library to find if a given string is numeric or not. The following example will give you a clear idea about the valid numeric values.

For example, 121, 12321, and 1221 if entered as string are valid numeric values.

Algorithm

  • Step 1 − The isNumeric function using reads 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. In the main function, the string is passed to the isNumeric function. The result of the function is used to print a message indicating whether the string represents a valid numeric value or not.

  • Step 3 − The variable named, “str” is being initialized. It will hold the string which is to be checked whether it is numeric or not.

  • Step 4 − The result is printed to the console using ‘putStrLn’ statement after the function is called.

Example 1

In this example, the isNumeric function uses the reads function to attempt to parse the input string as a numeric value. The reads function returns a list of pairs, where each pair consists of a parsed value and the remaining string after parsing. If the parsed value is a valid numeric value, the remaining string should be an empty string.

isNumeric :: String -> Bool
isNumeric str = case reads str :: [(Double, String)] of
               [(_, "")] -> True
               _         -> False
   
main :: IO ()
main = do
   let str = "1234"
   if isNumeric str
      then putStrLn "The string represents a valid numeric value."
      else putStrLn "The string does not represent a valid numeric value."

Output

The string represents a valid numeric value.

Example 2

In this example, the isNumeric function uses a regular expression pattern to check if the input string matches the syntax of a numeric value. The pattern "^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$" matches a string that starts with an optional sign (+ or -), followed by any number of digits (0-9), optionally followed by a decimal point and more digits, and optionally followed by an exponent specified as the letter e or E, an optional sign (+ or -), and more digits. If the passed string matches this pattern, the function returns True, indicating that the input string represents a valid numeric value. Otherwise, it returns False.

import Text.Regex.Posix

isNumeric :: String -> Bool
isNumeric str = str =~ "^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$"

main :: IO ()
main = do
   let str = "1234"
   if isNumeric str
      then putStrLn "The string represents a valid numeric value."
      else putStrLn "The string does not represent a valid numeric value."

Output

The string represents a valid numeric value.

Example 3

In this example, the isNumeric function uses a regular expression pattern to check if the input string matches the syntax of a numeric value. The pattern "^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$" matches a string that starts with an optional sign (+ or -), followed by any number of digits (0-9), optionally followed by a decimal point and more digits, and optionally followed by an exponent specified as the letter e or E, an optional sign (+ or -), and more digits. If the passed string matches this pattern, the function returns True, indicating that the input string represents a valid numeric value. Otherwise, it returns False.

import Data.Char

isNumeric :: String -> Bool
isNumeric [] = False
isNumeric str = all isDigit str ||
               (head str == '-' || head str == '+') && all isDigit (tail str) ||
               all isDigit (takeWhile isDigit str) &&
               head (dropWhile isDigit str) == '.' &&
               all isDigit (tail (dropWhile isDigit str))

main :: IO ()
main = do
   let str = "1234"
   if isNumeric str
      then putStrLn "The string represents a valid numeric value."
      else putStrLn "The string does not represent a valid numeric value."

Output

The string represents a valid numeric value.

Conclusion

In Haskell, we can check if a passed string is numeric or not using reads function, regular expressions or using Data.Char library.

Updated on: 14-Jul-2023

743 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements