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

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing variables for interest parameters</span>
   <span class="token keyword">let</span> <span class="token hvariable">a</span> <span class="token operator">=</span> <span class="token constant">True</span>
   <span class="token keyword">let</span> <span class="token hvariable">b</span> <span class="token operator">=</span> <span class="token constant">True</span>
   <span class="token keyword">let</span> <span class="token hvariable">c</span> <span class="token operator">=</span> <span class="token constant">False</span>
<span class="token comment">-- printing the status</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"Is the status of two boolean variables true?"</span><span class="token punctuation">)</span>
   <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token hvariable">a</span><span class="token operator">==</span><span class="token constant">True</span><span class="token punctuation">)</span>
   <span class="token keyword">then</span> <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token hvariable">b</span><span class="token operator">==</span><span class="token constant">True</span><span class="token punctuation">)</span>
      <span class="token keyword">then</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"yes"</span><span class="token punctuation">)</span>
      <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token hvariable">c</span><span class="token operator">==</span><span class="token constant">True</span><span class="token punctuation">)</span>
         <span class="token keyword">then</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"yes"</span><span class="token punctuation">)</span>
         <span class="token keyword">else</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"no"</span><span class="token punctuation">)</span>
   <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token hvariable">b</span><span class="token operator">==</span><span class="token constant">True</span><span class="token punctuation">)</span>
      <span class="token keyword">then</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token hvariable">c</span><span class="token operator">==</span><span class="token constant">True</span><span class="token punctuation">)</span>
         <span class="token keyword">then</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"yes"</span><span class="token punctuation">)</span>
         <span class="token keyword">else</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"no"</span><span class="token punctuation">)</span>
      <span class="token keyword">else</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"no"</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">-- function declaration for function check</span>
<span class="token hvariable">check</span> <span class="token operator">::</span><span class="token punctuation">[</span><span class="token constant">Bool</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token constant">Int</span>

<span class="token comment">-- function definition for function check</span>
<span class="token comment">-- base case</span>
<span class="token hvariable">check</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>
<span class="token hvariable">check</span> <span class="token punctuation">(</span><span class="token hvariable">x</span><span class="token operator">:</span><span class="token hvariable">xs</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token hvariable">x</span><span class="token punctuation">)</span>
   <span class="token keyword">then</span> <span class="token number">1</span> <span class="token operator">+</span> <span class="token hvariable">check</span> <span class="token hvariable">xs</span>
   <span class="token keyword">else</span> <span class="token hvariable">check</span> <span class="token hvariable">xs</span>
   
<span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing variables for interest parameters</span>
   <span class="token keyword">let</span> <span class="token hvariable">a</span> <span class="token operator">=</span> <span class="token constant">True</span>
   <span class="token keyword">let</span> <span class="token hvariable">b</span> <span class="token operator">=</span> <span class="token constant">False</span>
   <span class="token keyword">let</span> <span class="token hvariable">c</span> <span class="token operator">=</span> <span class="token constant">False</span>
<span class="token comment">-- computing the count of true values by invoking the function check</span>
   <span class="token keyword">let</span> <span class="token hvariable">status</span> <span class="token operator">=</span> <span class="token hvariable">check</span> <span class="token punctuation">[</span><span class="token hvariable">a</span><span class="token punctuation">,</span><span class="token hvariable">b</span><span class="token punctuation">,</span><span class="token hvariable">c</span><span class="token punctuation">]</span>
<span class="token comment">-- printing the status by evaluating the variable status</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"Is the status of two out of three boolean variables true?"</span><span class="token punctuation">)</span>
   <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token hvariable">status</span><span class="token operator">>=</span><span class="token number">2</span><span class="token punctuation">)</span>
   <span class="token keyword">then</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"yes"</span><span class="token punctuation">)</span>
   <span class="token keyword">else</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"no"</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-11-24T06:24:13+05:30

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements