Problem 46
Define functions to provide the logical binary functions and, or, nand, nor, xor, implies, and equivalent.
-- True if and only if a and b are true
and' : Bool -> Bool -> Bool
-- True if either a or b are true
or' : Bool -> Bool -> Bool
-- True either a or b are false
nand' : Bool -> Bool -> Bool
-- True if and only if a and b are false
nor' : Bool -> Bool -> Bool
-- True if a or b is true, but not if both are true
xor' : Bool -> Bool -> Bool
-- True if a is false or b is true
implies : Bool -> Bool -> Bool
-- True if both a and b are true, or both a and b ar false
equivalent : Bool -> Bool -> Bool
Define a function to show a truth table for a logical expression.
truthTable : (Bool -> Bool -> Bool) -> List (Bool, Bool, Bool)
Unit Test
import Html exposing (text)
import List exposing (map)
-- True if and only if a and b are true
and_ : Bool -> Bool -> Bool
and_ a b =
-- your implementation here
True
-- True if either a or b are true
or_ : Bool -> Bool -> Bool
or_ a b =
-- your implementation here
True
-- True either a or b are false
nand_ : Bool -> Bool -> Bool
nand_ a b =
-- your implementation here
True
-- True if and only if a and b are false
nor_ : Bool -> Bool -> Bool
nor_ a b =
-- your implementation here
True
-- True if a or b is true, but not if both are true
xor_ : Bool -> Bool -> Bool
xor_ a b =
-- your implementation here
True
-- True if a is false or b is true
implies : Bool -> Bool -> Bool
implies a b =
-- your implementation here
True
-- True if both a and b are true, or both a and b ar false
equivalent : Bool -> Bool -> Bool
equivalent a b =
-- your implementation here
True
truthTable : (Bool -> Bool -> Bool) -> List ( Bool, Bool, Bool )
truthTable f =
-- your implementation goes here
List.repeat 4 ( True, True, True )
main =
text
(if (test) then
"Your implementation passed all tests."
else
"Your implementation failed at least one test."
)
test : Bool
test =
List.all ((==) True)
[ truthTable and_
== [ ( True, True, True )
, ( True, False, False )
, ( False, True, False )
, ( False, False, False )
]
, truthTable or_
== [ ( True, True, True )
, ( True, False, True )
, ( False, True, True )
, ( False, False, False )
]
, truthTable nand_
== [ ( True, True, False )
, ( True, False, True )
, ( False, True, True )
, ( False, False, True )
]
, truthTable nor_
== [ ( True, True, False )
, ( True, False, False )
, ( False, True, False )
, ( False, False, True )
]
, truthTable xor_
== [ ( True, True, False )
, ( True, False, True )
, ( False, True, True )
, ( False, False, False )
]
, truthTable implies
== [ ( True, True, True )
, ( True, False, False )
, ( False, True, True )
, ( False, False, True )
]
, truthTable equivalent
== [ ( True, True, True )
, ( True, False, False )
, ( False, True, False )
, ( False, False, True )
]
, truthTable (\a b -> (and_ a (or_ a b)))
== [ ( True, True, True )
, ( True, False, True )
, ( False, True, False )
, ( False, False, False )
]
]