How to resolve the algorithm Determine if a string is numeric step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Determine if a string is numeric step by step in the Haskell programming language

Table of Contents

Problem Statement

Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine if a string is numeric step by step in the Haskell programming language

Type Signatures

isInteger :: String -> Bool
isDouble :: String -> Bool
isNumeric :: String -> Bool

Pattern Matching

isInteger s = case reads s :: [(Integer, String)] of
 [(_, "")] -> True
 _         -> False
  • reads attempts to parse the input string s into a list of tuples [(Integer, String)].
  • Each tuple contains an Integer value and a remaining string.
  • The pattern [(_, "")] matches a single tuple where the Integer is bound by the wildcard _ and the remaining string is empty.
  • If the pattern matches, the function returns True indicating that the input is a valid Integer.
  • Otherwise, it returns False.

Guards and Predicates

areDigits = all isDigit
isDigit  selects ASCII digits i.e. '0'..'9'
isOctDigit selects '0'..'7'
isHexDigit selects '0'..'9','A'..'F','a'..'f'
  • all applies a predicate to each element of a list and returns True if all elements satisfy the predicate.
  • isDigit checks if a character is an ASCII digit ('0' to '9').
  • isOctDigit checks if a character is an ASCII digit from '0' to '7'.
  • isHexDigit checks if a character is an ASCII digit from '0' to '9', or an uppercase or lowercase letter from 'A' to 'F'.

Combining Functions

isNumeric :: String -> Bool
isNumeric s = isInteger s || isDouble s
  • isNumeric combines the isInteger and isDouble functions.
  • It checks if a string s is either an Integer or a Double using || (logical OR).
  • It returns True if either check succeeds, and False otherwise.

Source code in the haskell programming language

isInteger s = case reads s :: [(Integer, String)] of
  [(_, "")] -> True
  _         -> False

isDouble s = case reads s :: [(Double, String)] of
  [(_, "")] -> True
  _         -> False

isNumeric :: String -> Bool
isNumeric s = isInteger s || isDouble s


areDigits = all isDigit
isDigit  selects ASCII digits i.e. '0'..'9'
isOctDigit selects '0'..'7'
isHexDigit selects '0'..'9','A'..'F','a'..'f'


  

You may also check:How to resolve the algorithm Compare length of two strings step by step in the Factor programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Tree from nesting levels step by step in the C++ programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Huginn programming language
You may also check:How to resolve the algorithm Boustrophedon transform step by step in the Perl programming language