xor和or在Haskell中有什么区别 [英] what is the difference between xor and or in Haskell

查看:207
本文介绍了xor和or在Haskell中有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,如果xor返回True,且仅当它的一个操作数正好是True时,才:

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

我的问题是: or是否相似,但是如果其一个或两个操作数为True,则返回True?

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

推荐答案

您可以简单地将xor编写为

 xor a b = a /= b

还可以简化or

 or True _ = True
 or False b = b

为了娱乐,您可以使用nand定义所有逻辑功能.

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

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

not很简单

 not a = nand a a

and需要两个门

 and a b = not (nand a b)

or,需要三个

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

并且xor需要四个

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

这篇关于xor和or在Haskell中有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆