gpt4 book ai didi

operators - 减少元操作符不一致

转载 作者:行者123 更新时间:2023-12-04 10:02:42 25 4
gpt4 key购买 nike

当我们检查reduce函数时:

    my $result = reduce &reduction_procedure, @array;

我们总结了以下内部运作的简单规则:
    Reduction Rules
---------------
1. For the first pair of objects from our input (@array)
we apply the reduction procedure (&reduction_procedure) and we get our first result.

2. We apply the reduction procedure (&reduction_procedure) to the result (object)
of the previous rule and the next input (object) from our input (@array),
and we get an intermediate result (object).

3. We run rule.2 for every of the remaining objects from our input (@array)

这个简单的规则对于归约元操作符 [] 也同样适用。例如:
    example.1
---------
say [+] [1,2,3,4]; # result : 10

例如 1 减少规则按原样适用:
    Step.1 use Rule.1 : 1 + 2 = 3     1(first value)     + 2(second value)  = 3
Step.2 use Rule.2 : 3 + 3 = 6 3(previous result) + 3(current value) = 6
Step.3 use Rule.2 : 6 + 4 = 10 6(previous result) + 4(current value) = 10

但不适用于以下示例:
    example.2
----------
say [<] 1,2,3,4; # result : True

例如.2我们观察到不一致:
    Step.1 use Rule.1 : 1 < 2 = True    1(first value)         <  2(second value)      = True
Step.2 use Rule.2 : 2 < 3 = True True(previous result) && True(current result) = True
Step.3 use Rule.2 : 3 < 4 = True True(previous result) && True(current result) = True(result)

不一致的是从Step.2开始,我们不能使用上一步的结果作为后续reduce操作的第一个参数,
但相反,我们使用实际值计算逻辑中间结果,最后我们在最后一步添加了“逻辑与”的使用
每一步的中间逻辑结果:
    Reduction Result = True && True && True = True (use of logical AND as "meta-reduction" operator)

可以说我们有一个“元还原”元操作符!

问题:
    1. Is that an inconsistency with a purpose?

2. Can we exploit this behavior? For instance instead of use of && (logical AND)
as "meta-reduction" operator can we use || (logical OR) or ?^ (logical XOR) ?

最佳答案

这不是不一致,而是如何operator associativity 的示例在乐工作。

写作 :

[<] 1,2,3,4

与写作相同:
1 < 2 < 3 < 4

在大多数语言中,这不起作用,但 < 运算符具有链关联性,因此它有效地将其视为:
(1 < 2) and (2 < 3) and (3 < 4)

这是真的。

关于operators - 减少元操作符不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59399554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com