• 请不要在回答技术问题时复制粘贴 AI 生成的内容
KentY
V2EX  ›  程序员

你不假思索第一反应回答: "Either A or B" 翻译成开发中的逻辑关系是?

  •  
  •   KentY ·
    sk1418 · Jan 26, 2017 · 10657 views
    This topic created in 3572 days ago, the information mentioned may be changed or developed.

    今天被同事问到,然后,我答错了... :-( 被告知答案后..服气.

    57 replies  •  2017-01-28 23:30:22 +08:00
    dongdongkun
    dongdongkun
    Jan 26, 2017
    1
    if A
    else B
    cxbig
    cxbig
    Jan 26, 2017
    2
    A || B
    KentY
    KentY
    OP
    Jan 26, 2017
    3
    @dongdongkun 您这不是逻辑关系, 问题是需要个 logical operation
    KentY
    KentY
    OP
    Jan 26, 2017
    4
    @cxbig 跟我回答的一样....
    codecrash
    codecrash
    Jan 26, 2017 via Android
    5
    (A&&!B)∥(!A&&B)
    justyy
    justyy
    Jan 26, 2017
    6
    A | B

    英文中 Either 的意思是 两者其中只要有一个满足就可以。
    所以可以存在的情况是, A 为真, B 为真, 或者 A 和 B 都为真。

    上面写的 if 肯定是不对的,
    || 有可能因为编译器 的 boolean 短路, 所以也不完全对。
    ynyounuo
    ynyounuo
    Jan 26, 2017❤️3
    7
    20015jjw
    20015jjw
    Jan 26, 2017 via Android
    8
    就是 A or B 啊... AB 中至少对一个就对
    不仅我们 CS 是这么教的,我们哲学里的逻辑也是这么教的。
    20015jjw
    20015jjw
    Jan 26, 2017 via Android
    9
    @justyy 短路为什么不对... 不还是对的么... A 对 a || b 就肯定是对的了呀
    langjiyuan
    langjiyuan
    Jan 26, 2017
    10
    a 或 b a | b 但是楼主答案是什么呢?强迫症要命啊
    chairuosen
    chairuosen
    Jan 26, 2017
    11
    不是或么?或就是||啊
    linbiaye
    linbiaye
    Jan 26, 2017
    12
    if ((a && !b) || (!a && b))
    justyy
    justyy
    Jan 26, 2017
    13
    @20015jjw
    A || B ==> 编译器 先检查 A , 如果为真, 就不会管 B 的值了(假设 B 有可能在真和假之间)
    A | B ==> 涵盖 A 为真 , B 为真 或者 A 和 B 都为真。。
    好吧。。明白你的意思,是我有点较真了。
    KentY
    KentY
    OP
    Jan 26, 2017
    14
    @ynyounuo 答的对, 应该是 XOR ,

    "either a or b" 意思是: A B 之中有且仅有一个是真.
    KentY
    KentY
    OP
    Jan 26, 2017❤️1
    15
    刚忘记了引用一下:

    either-or
    adjective [ before noun ] UK ​ /ˌaɪ.ðərˈɔːr/ /ˌiː.ðərˈɔːr/ US ​ /ˌiː.ðɚˈɔːr/ /ˌaɪ.ðɚˈɔːr/
    ​
    used to refer to a situation in which there is a choice between two different plans of action, but both together are not possible:
    It's an either-or situation - we can buy a new car this year or we can go on holiday, but we can't do both.
    laoyur
    laoyur
    Jan 26, 2017❤️1
    16
    rand() % 2 == 0 ? A : B
    justyy
    justyy
    Jan 26, 2017
    17
    @KentY 多谢, 原来我一直理解的是错的.
    FurN1
    FurN1
    Jan 26, 2017
    18
    异或。。。你得港清楚这是逻辑运算还是。。。
    ho121
    ho121
    Jan 26, 2017
    19
    either A or B 使用的语境一般是在 A 和 B 有且只有其中一个为真(而且不会有 A 和 B 之外的可能)的情况下,比如说:

    A red-black tree is a binary search tree with one extra bit of storage per node: its
    color, which can be either RED or BLACK.

    要储存一个节点的红黑属性,只需要一个 Boolean 变量就足够了,非黑即红。

    但是这似乎没法直接翻译成一个逻辑表达式。
    21grams
    21grams
    Jan 26, 2017
    20
    其实就是异或, A^B
    KentY
    KentY
    OP
    Jan 26, 2017❤️2
    21
    @ho121 在你说的这个 context 里, 肯定是非红即黑, 但是 either A or B 有可能判定 4 种情况的结果, 就是 AB 分别为 TT, TF, FF, FT, A xor B 可以根据这四种输入来做出判断.

    这个问题的由来是, 我在公司做了个东西, 简单说, 就是个用户的输入判断, 有 4 个输入框, 分两组(A B) (C D), 要求是要么 AB 都填, 要么 CD 都填, 当然全空或者穿插缺项, E.g. (AC or BD) 是要提示错误.

    然后同事在使用中发现, 如果 ABCD 全填了, 我就处理 AB 被填的那个逻辑去了, 他觉得应该是提示错误信息, 我说这不是个 or 的关系吗, 他说, it is not an "or" relation, instead it is an "either-or" relation. 我说, 对啊, 那不也是 logical OR 吗? 他说 are you sure? it is XOR.. 我仔细一想, 人家说的对啊......
    xuqd
    xuqd
    Jan 26, 2017
    22
    Either[A,B]
    Mes0
    Mes0
    Jan 26, 2017 via iPhone
    23
    异或吧
    circsqua
    circsqua
    Jan 26, 2017
    24
    异或
    laoyuan
    laoyuan
    Jan 26, 2017
    25
    要么 A 要么 B
    yangff
    yangff
    Jan 26, 2017❤️1
    26
    这种情况说 Exactly one 或者 One and Only One 比较合适吧

    至于 either 并不能很准确的表示二者之一这个意思……
    loading
    loading
    Jan 26, 2017 via Android
    27
    这个其实是英语题吧……
    yangff
    yangff
    Jan 26, 2017
    28
    Determiner[edit]
    either
    Each of two. [from 9th c.]  [quotations ▼]
    One or the other of two. [from 14th c.]  [quotations ▼]
    (coordinating) Used before two or more **not necessarily exclusive possibilities separated by "or"** or sometimes by a comma.  [quotations ▼]

    https://en.wiktionary.org/wiki/either
    FurN1
    FurN1
    Jan 26, 2017❤️1
    29
    @loading 其实是英语题,本来说啥都对的啊,后来这个问题被问得多了,连我们 CA 老师上课时候都讲这个逗我们,现在在贵圈 either 的意思就缩小成这样了……
    realpg
    realpg
    Jan 26, 2017
    30
    上技术社区考了一个英语题……
    hx1997
    hx1997
    Jan 26, 2017❤️1
    31
    https://en.wikipedia.org/wiki/Either/Or_(disambiguation)

    Either/Or and related terms may also refer to:

    Logical disjunction, the logical meaning of "either ... or ... or both"
    Exclusive or, the logical meaning of "either ... or ... but not both"

    可以指逻辑析取(逻辑或),也可以指异或。这个其实有歧义。
    codelegant
    codelegant
    Jan 26, 2017 via Android
    32
    这是一道英语题。
    ZE3kr
    ZE3kr
    Jan 26, 2017 via iPhone
    33
    平常所说的的 or ,中文的或,大多数情况下都是编程语言中的 xor ,这点很坑。
    ZE3kr
    ZE3kr
    Jan 26, 2017 via iPhone
    34
    @ynyounuo 这个其实是集合的运算符,不过其实都差不多
    edimetia3d
    edimetia3d
    Jan 26, 2017
    35
    各位英语捉急啊,英语中 either 的逻辑与 both 意义等价. Either A or B 的逻辑意义是 A 和 B 必须都为真.若任何一个为假,则不能用 either
    所以是 A and B
    FurN1
    FurN1
    Jan 26, 2017
    36
    @edimetia3d 喷了,哥们你是在钓鱼对吧
    PythonAnswer
    PythonAnswer
    Jan 26, 2017
    37
    @edimetia3d 我觉得 either 和 both 是不同的。

    either A or B == both A and B?
    FurN1
    FurN1
    Jan 26, 2017
    38
    @PythonAnswer 你被钓鱼了。。。
    edimetia3d
    edimetia3d
    Jan 26, 2017
    39
    @IgniteWhite
    @PythonAnswer
    开玩笑,各位这么认真,语言上的东西没必要这么较真啊.

    从语法的理论上讲,这样分析应该是没错的.
    陈述句举例: Either this apple or that apple is bad == Both apples are bad.

    实际应用中最正确的回答是"随语境".
    因为 either or 可以用在陈述句中使用,也可以用在选择句中,而有的选择句中强调必须择其一,有的则不限制必须择其一.

    就像"i++i+i++i+++i"的结果一样,答案是随编译器而定的
    FurN1
    FurN1
    Jan 27, 2017
    40
    @edimetia3d 哥你不是在开玩笑的样子……

    你在说 and 和 or 没区别,视编译器而定,是吗
    xupefei
    xupefei
    Jan 27, 2017❤️1
    41
    @edimetia3d
    ```
    Either this apple or that apple is bad == Both apples are bad.
    ```
    Are you serious?
    lyz1990
    lyz1990
    Jan 27, 2017 via Android
    42
    data Either a b = Left a | Right b
    scnace
    scnace
    Jan 27, 2017 via Android
    43
    所以直译应该是 不是 A 就是 B 吗?这样好像就排除掉 Both True 的 case 了(突然觉得中文博大精深啊!
    ynyounuo
    ynyounuo
    Jan 27, 2017 via iPhone
    44
    @ZE3kr 有些 formal logic language 是以 \oplus 作为 XOR 的逻辑符号的嘛
    RqPS6rhmP3Nyn3Tm
    RqPS6rhmP3Nyn3Tm
    Jan 27, 2017 via iPhone
    45
    A V B
    /or 符号打不出来,用 V 代替。离散数学基础啊兄弟们
    RqPS6rhmP3Nyn3Tm
    RqPS6rhmP3Nyn3Tm
    Jan 27, 2017 via iPhone
    46
    如果是 exclusive or 一般都会注明的,默认就是||我觉得没问题
    ynyounuo
    ynyounuo
    Jan 27, 2017
    47
    @BXIA \lor 或者 \vee 可以打 ∨ (U+2228)
    icylogic
    icylogic
    Jan 27, 2017
    48
    stack exchange 上的两个答案

    >It isn't always actually used with full precision, though, so, as usual, context has to be taken into account. If somebody says, "select either A or B", for example, they definitely mean that you should not select both. If they say "if either A or B is true", though, they probably mean a non-exclusive OR, and the condition is still true if both A and B are true.

    >Usually, the inclusive sense is used in mathematics and the exclusive sense in everyday life. In any case, further specification or context will remove any doubt.

    所以如果是 choose either A or B 这样那肯定就是 xor 了,但其他情况看语境也可能与 or 同义


    还有 math.toronto.edu 给了同样的解释:

    >In every day language we use the phrase "either A or B" to mean that one of the two options holds, but not both.For example, when most people say something like ``You can have either a hot dog or hamburger," they usually aren't offering you both. The use of ``either/or" in everyday English is usually divisive, and meant to imply there are only two options: A or B, but not both A and B. (The use of "or" in this way is sometimes referred to as "exclusive or.")

    >However, the use of "either A or B" in mathematics allows the option that both A and B hold. (The use of "or" in this way is sometimes referred to as "inclusive or.") For example, in mathematics, the statement "If x is a real number, then either x≤0 or x≥0" allows the possibility that x satisfies both x≤0, as well as x≥0 (which is true of the real number 0).
    icylogic
    icylogic
    Jan 27, 2017
    49
    特别是 "either A or B is true" 这种判断逻辑的情况,很有可能应该等同于 or 。

    https://en.wikipedia.org/wiki/Logical_disjunction#Natural_language

    **On the other hand, "Her grades are so good that she's either very bright or studies hard" does not exclude the possibility of both.**
    eyp82
    eyp82
    Jan 27, 2017
    50
    我的第一反应就是: A or B ... (Python)
    tt7
    tt7
    Jan 27, 2017
    51
    assert a or b
    Balthild
    Balthild
    Jan 27, 2017 via Android
    52
    bool either(bool A, bool B);

    either(A, B);
    czk1997
    czk1997
    Jan 27, 2017
    53
    根据剑桥的解释:
    used to refer to a situation in which there is a choice between two different plans of action, but both together are not possible:
    It's an either-or situation - we can buy a new car this year or we can go on holiday, but we can't do both.
    就是可以其中一个为真,结果为真,但是不能都为真。
    halden
    halden
    Jan 27, 2017
    54
    凭我的经验英文中的 either 不带 both ,老美会说 either A or B, or both 。所以应该是 (A && !B) || (!A && B)
    thekll
    thekll
    Jan 27, 2017
    55
    !A != !B
    precisi0nux
    precisi0nux
    Jan 28, 2017
    56
    $a or $b|Or|TRUE if either $a or $b is TRUE.
    $a xor $b|Xor|TRUE if either $a or $b is TRUE, but not both.

    世界上最好的语言如是说。
    nozama
    nozama
    Jan 28, 2017
    57
    xor
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   889 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 102ms · UTC 18:11 · PVG 02:11 · LAX 10:11 · JFK 13:11
    ♥ Do have faith in what you're doing.