在rise4fun的Dafny教程中,s[i := v]
定义为按序列i
中的v
替换索引s
。
但是使用它总是失败expected method call, found expression
。
例如在下面的代码中交换两个索引
var a:int :=input[j];
var b:int :=input[j-1];
input[j := b]; //expected method call, found expression
input[j-1 := a]; //expected method call, found expression
在交换两个索引的情况下使用s[i := v]
的正确方法是什么。
答案 0 :(得分:3)
你可以写
var a:int :=input[j];
var b:int :=input[j-1];
input := input[j:=b] ;
input := input[j-i : a];
更简洁,但也许更难阅读
input := input[ j := input[j-1] ][ j-1 := input[j] ] ;