Haskell Program to convert string type variables into boolean


In Haskell, we will convert string type variables into boolean by using user-defined function, stringToBool along with Maybe Bool, toLower and readMaybe functions. In the first example, we are going to use (stringToBool :: String -> Maybe Bool) function and in the second example, we are going to use (stringToBool str = case map toLower str of). And in third example, we are going to use (stringToBool str = case readMaybe str of).

Algorithm

  • Step 1 − The stringToBool function is defined.

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

  • Step 4 − The function, stringToBool is being called and str is passed to it.

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

Example 1

In this example, the stringToBool function is defined, which takes a string as input and returns a Maybe Bool value. The main function reads in the input, and uses the stringToBool function to attempt to convert the input string to a boolean. If the conversion succeeds, the resulting boolean value is printed to the console using putStrLn and show. If the conversion fails, the program prints an error message.

module Main where

stringToBool :: String -> Maybe Bool
stringToBool "true"  = Just True
stringToBool "false" = Just False
stringToBool _       = Nothing

main :: IO ()
main = do
   let str = "true"
   case stringToBool str of
      Just b  -> putStrLn ("Boolean value is: " ++ show b)
      Nothing -> putStrLn "Invalid input"

Output

Boolean value is: True

Example 2

In this example, the input string is first converted to lowercase using map toLower. It then checks if the lowercase string is equal to "true" or "false". If it is, the corresponding boolean value is returned wrapped in a Just constructor; otherwise, Nothing is returned.

import Data.Char (toLower)

stringToBool :: String -> Maybe Bool
stringToBool str = case map toLower str of
   "true"  -> Just True
   "false" -> Just False
   _       -> Nothing
   
main :: IO ()
main = do
   let str = "true"
   case stringToBool str of
      Just b  -> putStrLn ("Boolean value is: " ++ show b)
      Nothing -> putStrLn "Invalid input"

Output

Boolean value is: True

Example 3

In this example, the readMaybe function is used to attempt to parse the input string as a boolean value. If the parsing is successful and the result is True or False, the corresponding boolean value is wrapped in a Just constructor and returned. Otherwise, Nothing is returned.

import Text.Read (readMaybe)

stringToBool :: String -> Maybe Bool
stringToBool str = case readMaybe str of
   Just True -> Just True
   Just False -> Just False
   _ -> Nothing

main :: IO ()
main = do
   let str = "True"
   case stringToBool str of
      Just b  -> putStrLn ("Boolean value is: " ++ show b)
      Nothing -> putStrLn "Invalid input"

Output

Boolean value is: True

Conclusion

String to boolean conversion is the process of converting a string representation of a boolean value (such as "True" or "False") into an actual boolean value in a programming language. Booleans are a fundamental data type in programming languages that represent two possible states: true or false. In some cases, user input or data may be in the form of a string that represents a boolean value, and it may be necessary to convert this string into an actual boolean value that can be used in code. In Haskell, a string variable is converted to boolean using user-defined stringToBool function along with toLower and readMaybe functions.

Updated on: 20-Apr-2023

285 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements