Haskell Program to convert string variables to double


In Haskell, we will convert string type variables into double by using user-defined function, stringToDouble along with read annotation, readMaybe and double functions. In the first example, we are going to use ( let value = read input :: Double) and in the second example, we are going to use (stringToDouble s = readMaybe s). And in the third example, we are going to use (stringToDouble s = case double $ pack s of).

Algorithm

  • Step 1 − 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 2 − The variable named, “input” is being initialized. It will hold the string value that is to be converted to respective double value.

  • Step 3 − The read annotation is called for double value.

  • Step 4 − The resultant double value is printed to the console using ‘putStrLn’ statement.

Example 1

In this example, the :: Double after read input is a type annotation that tells Haskell that we want to convert the input string to a double. The resulting double value is then printed using putStrLn and show.

main :: IO ()
main = do
   let input = "125.34"
   let value = read input :: Double
   putStrLn $ "The double value is: " ++ show value

Output

The double value is: 125.34

Example 2

In this example, the readMaybe function from the Text.Read module returns a Maybe value that is Just the parsed value if the parse was successful, or Nothing if it failed.

import Text.Read (readMaybe)

stringToDouble :: String -> Maybe Double
stringToDouble s = readMaybe s
main :: IO ()
main = do
   let input = "123.45"
   case stringToDouble input of
      Just value -> putStrLn $ "The double value is: " ++ show value
      Nothing -> putStrLn "Invalid input"

Output

The double value is: 123.45

Example 3

In this method, the double function returns either a Right value with the parsed Double and any remaining Text, or a Left value with an error message.

import Data.Text (pack)
import Data.Text.Read (double)

stringToDouble :: String -> Maybe Double
stringToDouble s = case double $ pack s of
   Right (d, _) -> Just d
   Left _ -> Nothing

main :: IO ()
main = do
   let input = "123.45"
   case stringToDouble input of
      Just value -> putStrLn $ "The double value is: " ++ show value
      Nothing -> putStrLn "Invalid input"

Output

The double value is: 123.45

Conclusion

String-to-double conversion is the process of converting a string representation of a numerical value (such as "3.14") to a double-precision floating-point value (also known as a double). In Haskell, you can use the read function to convert a string to a double. The read function takes a string and returns a value of a specified type, such as Double. If the string cannot be parsed as a valid double, an error will be thrown. It's also possible to use the readMaybe function or other parsing functions from the Text. Read and Data.Text.Read modules to handle errors more gracefully.

Updated on: 20-Apr-2023

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements