- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to convert boolean variables into string
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.
- Related Articles
- Haskell Program to convert string type variables into boolean
- Golang Program to convert boolean variables into string
- C++ Program to convert Boolean Variables into String
- C++ Program to Convert String Type Variables into Boolean
- Golang Program to convert string type variables into Boolean
- Haskell Program to convert string type variables into int
- Haskell Program to Convert Boolean to String
- Haskell Program to convert string variables to double
- Haskell Program to convert long type variables into int
- Haskell Program to convert int type variables to String
- Haskell Program to convert double type variables to string
- Swift program to convert boolean variable into string
- Swift program to convert string type variable into boolean
- Haskell Program to convert the string into an integer
- C++ Program to Convert int Type Variables into String
