Haskell Program to Check if two of the three Boolean variables are true


In this tutorial, we discuss writing a program to check if two of three Boolean variables are true in the Haskell programming language

Boolean variables are the type of variable which hold the boolean value true and false.

In this tutorial, we see two different ways to implement a program to check if two of the three boolean values are true

  • Program to check if two of the three boolean values are true in an iterative method.

  • Program to check if two of the three boolean values are true in a recursive method.

Algorithm steps

  • Declare or take input three boolean values.

  • Implement the program to check if two of the three boolean variables are true.

  • Print or Display the status.

Example 1

Program to check if two of the three boolean values are true in an iterative method

main = do -- declaring and initializing variables for interest parameters let a = True let b = True let c = False -- printing the status print ("Is the status of two boolean variables true?") if(a==True) then if( b==True) then print("yes") else if(c==True) then print("yes") else print("no") else if(b==True) then if(c==True) then print("yes") else print("no") else print ("no")

Output

"Is the status of two boolean variables true?"
"yes"

In the above program, We declared and initialized three variable a,b, and c with random boolean values.

  • The program checks the value of the first variable a

    • If the first variable is true, then it checks the second variable

      • If the second variable is also true the program prints “yes” as two Variable have true values.

      • Else If the second variable is not true, the program checks the third variable

        • If the third variable is true, then the program prints “yes” as two Variable have true values.

        • Else the third variable is not true, the program prints “no” as only one variable has the value true

    • Else the first variable is not true, the program checks the second variable

      • If the second variable is true then it checks the third variable.

        • If the third variable is true, the program prints “yes” as two variables have true values.

        • Else the third variable is not true, the program prints “no”, as only two of the three variables don't have true values.

      • Else the second variable is not true, the program prints “no” as two of the three variables don’t have true values.

Example 2

Program to check if two of the three boolean values are true in a recursive method

-- function declaration for function check check ::[Bool]->Int -- function definition for function check -- base case check [] = 0 check (x:xs) = if (x) then 1 + check xs else check xs main :: IO() main = do -- declaring and initializing variables for interest parameters let a = True let b = False let c = False -- computing the count of true values by invoking the function check let status = check [a,b,c] -- printing the status by evaluating the variable status print ("Is the status of two out of three boolean variables true?") if(status>=2) then print ("yes") else print ("no")

Output

"Is the status of two out of three boolean variables true?"
"no"

In the above program, we declared a function check which a list of boolean an argument and returns an integer. In the function definition, we are accepting an argument using pattern-matching syntax a list of elements. The function checks the first element if the first element is true the function returns an integer 1 addition with a recursive call to itself with the remaining list as an argument. If the element is not true, the function just returns a recursive function call to itself with the remaining list as an argument. The recursive calls are called until a base case is attained where the list is empty in which the function returns 0. I.e the function returns the count of true values in the list. In the main function, we declared and initialized values for the three variables a,b, and c. we invoked the function check with an argument a list containing these three variables, and load the returned count into a variable status. If the status is greater than or equal to 2 we are printing the status as “yes” else, we are printing the status as “no”.

Conclusion

In this tutorial, we discussed two different methods to implement a program to check if two of three boolean variables are true in Haskell programming Language.

Updated on: 24-Nov-2022

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements