Created
September 16, 2013 23:02
-
-
Save timjb/6587803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- http://math.stackexchange.com/questions/307201/how-to-verify-if-three-numbers-are-equal-using-logical-operators-with-a-restric?rq=1 | |
module Eq3Expr where | |
data Bit = Zero | One deriving (Eq, Show) | |
type Operator = Bit -> Bit -> Bit | |
notB :: Bit -> Bit | |
notB Zero = One | |
notB One = Zero | |
andB :: Operator | |
andB One One = One | |
andB _ _ = Zero | |
orB :: Operator | |
orB Zero Zero = Zero | |
orB _ _ = One | |
xorB :: Operator | |
xorB Zero One = One | |
xorB One Zero = One | |
xorB _ _ = Zero | |
operators :: [Operator] | |
operators = ops ++ map (\o a b -> notB (o a b)) ops | |
where ops = [andB, orB, xorB] | |
test :: Operator -> Operator -> Bool | |
test op1 op2 = all id $ do | |
a <- [Zero, One] | |
b <- [Zero, One] | |
c <- [Zero, One] | |
let allEqual = a == b && b == c | |
let expected = if allEqual then One else Zero | |
return $ (a `op1` b) `op2` c == expected | |
main :: IO () | |
main = print $ do | |
op1 <- operators | |
op2 <- operators | |
return $ test op1 op2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment