
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Boolean Variables into String in Haskell
In Haskell, we will convert boolean variables into string by using user-defined function, boolToString along with if-else statement and pattern matching. In the first example, we are going to use (boolToString b = show b) function and in the second example, we are going to use (boolToString b = if b then "yes" else "no"). And in third example, we are going to use pattern matching.
Algorithm
Step 1 ? Define the boolToString function
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, "boolVar" is being initialized. It will hold the boolean type variable that is to be converted to respective string value.
Step 4 ? The function, boolToString is being called and boolVar is passed to it.
Step 5 ? The resultant string value is printed to the console using ?putStrLn' statement after the function is called.
Example 1
In this example, the user-defined function, boolToString is defined to convert the boolean variables to string.
boolToString :: Bool -> String boolToString b = show b main :: IO () main = do let boolVar = True let strVar = boolToString boolVar putStrLn $ "The boolean value is " ++ show boolVar ++ "." putStrLn $ "The corresponding string value is " ++ strVar ++ "."
Output
The boolean value is True. The corresponding string value is True.
Example 2
In this example, the user-defined function, boolToString is defined using if-else statement to convert the boolean variables to string.
boolToString :: Bool -> String boolToString b = if b then "yes" else "no" main :: IO () main = do let boolVar = True let strVar = boolToString boolVar putStrLn $ "The boolean value is " ++ show boolVar ++ "." putStrLn $ "The corresponding string value is " ++ strVar ++ "."
Output
The boolean value is True. The corresponding string value is yes.
Example 3
In this example, the user-defined function, boolToString is defined using pattern matching to convert the boolean variables to string.
boolToString :: Bool -> String boolToString True = "yes" boolToString False = "no" main :: IO () main = do let boolVar = True let strVar = boolToString boolVar putStrLn $ "The boolean value is " ++ show boolVar ++ "." putStrLn $ "The corresponding string value is " ++ strVar ++ "."
Output
The boolean value is True. The corresponding string value is yes.
Conclusion
Boolean to string conversion is the process of converting a boolean value (i.e., true or false) to its corresponding string representation. In most programming languages, the string representation of true is typically "true" or "True", and the string representation of false is typically "false" or "False". In Haskell, a boolean variable is converted to string using user-defined boolToString function along with if-else statement and pattern matching.