Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating truth tables for logic expressions in Haskell
    text
    copied!<p>The first part is an evaluation function that has the following type signature:</p> <pre><code>evaluate :: Logic Expr -&gt; [(Variable, Bool)] -&gt; Bool </code></pre> <p>This takes a logic expression and a list of assignment pairs as input and returns the value of the expression according to the Boolean assignment provided. The assignment list is a distinct list of pairs where each pair contains a variable and its Boolean assignment. That is, if you pass to the function the expression A &and; B and the assignment A = 1 and B = 0, your function must return 0 (this comes from Digital Logic Design, 0 corresponds to false, and 1 corresponds to true).</p> <p>This is what I managed to do so far:</p> <pre><code>type Variable = Char data LogicExpr = V Variable | Negation LogicExpr | Conjunction LogicExpr LogicExpr | Disjunction LogicExpr LogicExpr | Implication LogicExpr LogicExpr evaluate :: LogicExpr -&gt; [(Variable,Bool)] -&gt; Bool evaluate (V a) ((x1,x2):xs) | a==x1 = x2 | otherwise = (evaluate(V a)xs) evaluate (Negation a) l | (evaluate a l)==True = False | otherwise = True evaluate (Conjunction a b) l = (evaluate a l)&amp;&amp;(evaluate b l) evaluate (Disjunction a b) l = (evaluate a l)||(evaluate b l) evaluate (Implication a b) l | (((evaluate b l)==False)&amp;&amp;((evaluate a l)==True)) = False | otherwise = True </code></pre> <p>The next part is to define <code>generateTruthTable</code>, which is a function that takes a logic expression as input and returns the truth table of the expression in the form of a list of lists of assignment pairs. That is, if you pass to the function the expression E = A &and; B, your function must return A = 0, B = 0, E = 0 | A = 0, B = 1, E = 0 | A = 1, B = 0, E = 0 | A = 1, B = 1, E = 1.</p> <p>I'm not exactly familiar with the syntax so I don't know how to return the list.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload