使用分号语法了解评估和执行顺序 [英] Understanding the evaluation and execution order with semicolon syntax

查看:83
本文介绍了使用分号语法了解评估和执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从我的上一个问题中学到了一些关于FFL分号的信息.但是,仍不清楚它们执行什么评估或执行顺序.因此,这是一个更具体的示例:

I already learned a little bit about FFL semicolons from my previous question. However, it is still not clear what order of evaluation or execution they enforce. So here is a more concrete example:

 [ expr_a, expr_b ; expr_c, expr_d ; expr_e, expr_f ]

以上代码的执行顺序应该是什么?在我的脑海中应该是:

What should be the order of execution for the above code? In my head, it should be:

  1. 评估& b
  2. 执行a,执行b
  3. 评估c& d
  4. 执行c,执行d
  5. 评估e& f
  6. 执行e,执行f

现在让我们想象一下expr_b = add(test_list, ['b saw ' + str(test_list)])以及所有其他表达式的相似之处.那么test_list的最终内容是什么?

Now let's imagine that expr_b = add(test_list, ['b saw ' + str(test_list)]) and similar for all the other expressions. Then what would be the final contents of test_list?

在我的脑海中应该是:

锯[]

b看到了[]

c看见了[a看见了[],b看见了[]]

c saw [a saw [], b saw []]

d锯[a锯[],b锯[]]

d saw [a saw [], b saw []]

e锯[a锯[],b锯[],c锯[a锯[],b锯[]],d锯[a锯[],b锯[]]]

e saw [a saw [], b saw [], c saw [a saw [], b saw []], d saw [a saw [], b saw []]]

f锯[a锯[],b锯[],c锯[a锯[],b锯[]],d锯[a锯[],b锯[]]]

f saw [a saw [], b saw [], c saw [a saw [], b saw []], d saw [a saw [], b saw []]]

请解释为什么不是这种情况.

Please explain why that is not the case.

推荐答案

首先,您可能不想编写完全像这样的代码.通常,分号的优先级非常低,但是列表文字不是运算符,并且代码将如下所示:

To begin with, you probably don't want to write code exactly like this. Generally, semi-colons have very low precedence, but a list literal isn't an operator, and the code will be seen like this:

[a, (b; c), (d; e), f]

这意味着您将并行启动四个命令管道(尽管其中两个只有一个成员).它将评估abdf.然后它将执行a的结果,然后是b的结果.执行b将触发命令管道中的下一步,因此它将评估并执行c.然后将执行d,然后求值并执行e,最后将执行f.

This means that you are starting off four command pipelines in parallel (though two of them only have a single member). It will evaluate a, b, d, f. Then it will execute the results of a, then the results of b. Executing b will trigger the next step in the command pipeline, so it will evaluate and execute c. Then it will execute d, then evaluate and execute e, finally it will execute f.

所以:

a saw []
b saw []
c saw [a saw [], b saw []]
d saw []
e saw [a saw [], b saw [], c saw [a saw [], b saw []], d saw []]
f saw []

这篇关于使用分号语法了解评估和执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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