V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
KentY
V2EX  ›  程序员

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

  •  
  •   KentY ·
    sk1418 · 2017-01-26 18:27:57 +08:00 · 8822 次点击
    这是一个创建于 2618 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    "either a or b" 意思是: A B 之中有且仅有一个是真.
    KentY
        15
    KentY  
    OP
       2017-01-26 19:16:56 +08:00   ❤️ 1
    刚忘记了引用一下:

    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
        16
    laoyur  
       2017-01-26 19:17:29 +08:00   ❤️ 1
    rand() % 2 == 0 ? A : B
    justyy
        17
    justyy  
       2017-01-26 19:17:51 +08:00
    @KentY 多谢, 原来我一直理解的是错的.
    IgniteWhite
        18
    IgniteWhite  
       2017-01-26 19:30:47 +08:00
    异或。。。你得港清楚这是逻辑运算还是。。。
    ho121
        19
    ho121  
       2017-01-26 19:37:06 +08:00
    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
        20
    21grams  
       2017-01-26 19:49:29 +08:00
    其实就是异或, A^B
    KentY
        21
    KentY  
    OP
       2017-01-26 19:49:39 +08:00   ❤️ 2
    @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
        22
    xuqd  
       2017-01-26 19:54:28 +08:00
    Either[A,B]
    Mes0
        23
    Mes0  
       2017-01-26 20:37:16 +08:00 via iPhone
    异或吧
    circsqua
        24
    circsqua  
       2017-01-26 20:39:23 +08:00
    异或
    laoyuan
        25
    laoyuan  
       2017-01-26 20:49:04 +08:00
    要么 A 要么 B
    yangff
        26
    yangff  
       2017-01-26 20:57:04 +08:00   ❤️ 1
    这种情况说 Exactly one 或者 One and Only One 比较合适吧

    至于 either 并不能很准确的表示二者之一这个意思……
    loading
        27
    loading  
       2017-01-26 20:57:16 +08:00 via Android
    这个其实是英语题吧……
    yangff
        28
    yangff  
       2017-01-26 21:01:50 +08:00
    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
    IgniteWhite
        29
    IgniteWhite  
       2017-01-26 21:05:24 +08:00   ❤️ 1
    @loading 其实是英语题,本来说啥都对的啊,后来这个问题被问得多了,连我们 CA 老师上课时候都讲这个逗我们,现在在贵圈 either 的意思就缩小成这样了……
    realpg
        30
    realpg  
       2017-01-26 21:28:15 +08:00
    上技术社区考了一个英语题……
    hx1997
        31
    hx1997  
       2017-01-26 21:53:02 +08:00   ❤️ 1
    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
        32
    codelegant  
       2017-01-26 22:45:54 +08:00 via Android
    这是一道英语题。
    ZE3kr
        33
    ZE3kr  
       2017-01-26 22:58:57 +08:00 via iPhone
    平常所说的的 or ,中文的或,大多数情况下都是编程语言中的 xor ,这点很坑。
    ZE3kr
        34
    ZE3kr  
       2017-01-26 23:00:58 +08:00 via iPhone
    @ynyounuo 这个其实是集合的运算符,不过其实都差不多
    edimetia3d
        35
    edimetia3d  
       2017-01-26 23:13:29 +08:00
    各位英语捉急啊,英语中 either 的逻辑与 both 意义等价. Either A or B 的逻辑意义是 A 和 B 必须都为真.若任何一个为假,则不能用 either
    所以是 A and B
    IgniteWhite
        36
    IgniteWhite  
       2017-01-26 23:16:43 +08:00
    @edimetia3d 喷了,哥们你是在钓鱼对吧
    PythonAnswer
        37
    PythonAnswer  
       2017-01-26 23:45:40 +08:00
    @edimetia3d 我觉得 either 和 both 是不同的。

    either A or B == both A and B?
    IgniteWhite
        38
    IgniteWhite  
       2017-01-26 23:56:05 +08:00
    @PythonAnswer 你被钓鱼了。。。
    edimetia3d
        39
    edimetia3d  
       2017-01-26 23:57:42 +08:00
    @IgniteWhite
    @PythonAnswer
    开玩笑,各位这么认真,语言上的东西没必要这么较真啊.

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

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

    就像"i++i+i++i+++i"的结果一样,答案是随编译器而定的
    IgniteWhite
        40
    IgniteWhite  
       2017-01-27 00:04:14 +08:00
    @edimetia3d 哥你不是在开玩笑的样子……

    你在说 and 和 or 没区别,视编译器而定,是吗
    xupefei
        41
    xupefei  
       2017-01-27 00:07:59 +08:00   ❤️ 1
    @edimetia3d
    ```
    Either this apple or that apple is bad == Both apples are bad.
    ```
    Are you serious?
    lyz1990
        42
    lyz1990  
       2017-01-27 00:42:01 +08:00 via Android
    data Either a b = Left a | Right b
    scnace
        43
    scnace  
       2017-01-27 02:23:43 +08:00 via Android
    所以直译应该是 不是 A 就是 B 吗?这样好像就排除掉 Both True 的 case 了(突然觉得中文博大精深啊!
    ynyounuo
        44
    ynyounuo  
       2017-01-27 02:32:00 +08:00 via iPhone
    @ZE3kr 有些 formal logic language 是以 \oplus 作为 XOR 的逻辑符号的嘛
    RqPS6rhmP3Nyn3Tm
        45
    RqPS6rhmP3Nyn3Tm  
       2017-01-27 03:09:30 +08:00 via iPhone
    A V B
    /or 符号打不出来,用 V 代替。离散数学基础啊兄弟们
    RqPS6rhmP3Nyn3Tm
        46
    RqPS6rhmP3Nyn3Tm  
       2017-01-27 03:10:50 +08:00 via iPhone
    如果是 exclusive or 一般都会注明的,默认就是||我觉得没问题
    ynyounuo
        47
    ynyounuo  
       2017-01-27 03:49:09 +08:00
    @BXIA \lor 或者 \vee 可以打 ∨ (U+2228)
    icylogic
        48
    icylogic  
       2017-01-27 04:08:59 +08:00
    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
        49
    icylogic  
       2017-01-27 04:12:14 +08:00
    特别是 "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
        50
    eyp82  
       2017-01-27 04:19:24 +08:00
    我的第一反应就是: A or B ... (Python)
    tt7
        51
    tt7  
       2017-01-27 04:30:01 +08:00
    assert a or b
    Balthild
        52
    Balthild  
       2017-01-27 08:27:23 +08:00 via Android
    bool either(bool A, bool B);

    either(A, B);
    czk1997
        53
    czk1997  
       2017-01-27 12:25:46 +08:00
    根据剑桥的解释:
    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
        54
    halden  
       2017-01-27 13:38:46 +08:00
    凭我的经验英文中的 either 不带 both ,老美会说 either A or B, or both 。所以应该是 (A && !B) || (!A && B)
    thekll
        55
    thekll  
       2017-01-27 22:43:27 +08:00
    !A != !B
    precisi0nux
        56
    precisi0nux  
       2017-01-28 10:16:53 +08:00
    $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
        57
    nozama  
       2017-01-28 23:30:22 +08:00
    xor
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5321 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 09:04 · PVG 17:04 · LAX 02:04 · JFK 05:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.