如何使用运算符获取 TLA+/PlusCal 中序列元素的总和?
How to get sum of sequence elements in TLA+/PlusCal using operator?
想到要写这样的东西:
Sum(seq) ==
LET F[m \subseteq {seq[x]: x \in DOMAIN seq}] ==
IF Head(seq) = <<>> THEN 0
ELSE Head(seq) + F[Tail(seq)]
IN F[seq]
..但不是这样的。
LET seq == <<1,2,3,4,5,3,5>>
Sum[ i \in 1..Len(seq) ] == IF i = 1 THEN seq[i] ELSE seq[i] + Sum[i-1]
IN IF seq = <<>> THEN 0 ELSE Sum[Len(seq)]
想到要写这样的东西:
Sum(seq) ==
LET F[m \subseteq {seq[x]: x \in DOMAIN seq}] ==
IF Head(seq) = <<>> THEN 0
ELSE Head(seq) + F[Tail(seq)]
IN F[seq]
..但不是这样的。
LET seq == <<1,2,3,4,5,3,5>>
Sum[ i \in 1..Len(seq) ] == IF i = 1 THEN seq[i] ELSE seq[i] + Sum[i-1]
IN IF seq = <<>> THEN 0 ELSE Sum[Len(seq)]