我对forall
语句的理解是它们是并行执行的,而for
语句是串行执行的。实际上,以下代码似乎证实了这种期望(即,仅用于forall
线程的随机序列):
for i in 1..5 do writeln( i * 10 );
10
20
30
40
50
forall i in 1..5 do writeln( i * 10 );
10
50
20
30
40
另一方面,如果我在右侧使用forall
(或等效的 [...] )作为表达式
var A = ( forall i in 1..5 do i * 10 );
var B = [ i in 1..5 ] i * 10;
var X = ( forall a in A do a );
var Y = [ a in A ] a;
var P = ( for i in 1..5 do i * 10 ); // for comparison
writeln( "A = ", A );
writeln( "B = ", B );
writeln( "X = ", X );
writeln( "Y = ", Y );
writeln( "P = ", P );
所有结果都变得相同(即从 10 到 50 排序):
A = 10 20 30 40 50
B = 10 20 30 40 50
X = 10 20 30 40 50
Y = 10 20 30 40 50
P = 10 20 30 40 50
这是否意味着forall
赋值右侧的表达式总是串行执行?如果是这样,相应的 [...] 是否也等同for
于该上下文中的表达式?