简体   繁体   中英

what is the difference between xor and or in Haskell

So, if xor returns True if and only if exactly one of its operands is True such that:

xor :: Bool -> Bool -> Bool
xor True True = False
xor True False = True
xor False True = True
xor False False = False

My question is: would or be similar but return True if one or both of its operands is True ?

you can write xor simply as

 xor a b = a /= b

also or can be simplified

 or True _ = True
 or False b = b

For fun, you can define all logical functions in terms of nand.

 nand :: Bool -> Bool -> Bool
 nand True True = False
 nand _ _ = True

not is easy

 not a = nand a a

and requires two gates

 and a b = not (nand a b)

or , requires three

 or a b = nand (not a) (not b)  

and, xor requires four

 xor a b = let z = nand a b in nand (nand z a) (nand z b) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM