dafny - Dafny使用递归函数查找序列索引
问题描述
我正在做达夫尼练习。我编写了一个递归函数来返回以双精度形式出现的元素序列。现在我想使用另一个递归函数找到这些双精度元素的索引。但我有点卡住了。
例如,在序列[1,4,2,2,4,6,6]中,双精度序列应该是[2,6],索引序列应该是[3,6],也就是最后一个双精度元素的索引.
这是我查找双打序列的代码:
function method doubles(s:seq<int>) :(r:seq<int>)
requires |s|>=0
ensures |r|<=|s|
ensures |r|>=0
ensures forall i:int :: 0 <= i <|r| ==> r[i] in s
decreases s
{
if(|s|==1 || |s|==0) then []
else if (s[0]==s[1]) then [s[1]] + doubles(s[1..])
else doubles(s[1..])
}
并找到双元素的索引。它现在不起作用:
function method doublesIndex(s:seq<int>) :(r:seq<int>)
requires |s|>=0
ensures |r|<=|s|
ensures |r|>=0
decreases s
{
if(|s|==1 || |s|==0) then [0]
else if (s[0]==s[1]) then [s[1]] + doublesIndex(s[1..])
else doublesIndex(s[1..])
}
解决方案
推荐阅读
- firebase - 如果有人窃取了您的 GoogleInfo.plist 并一直创建 Firebase 用户怎么办?[Firestore、Firebase 身份验证、安全规则]
- r - 迭代地计算 data.table 的列,一次一行(递归列定义)
- azure-devops - 从 Multistage YAML 中的其他管道下载工件
- python - 具有嵌套过滤器和 AND 和 OR 条件的 Elasticsearch-dsl 完全匹配
- python - python matplotlib y轴移动图像
- javascript - 组件在 Next.js/React 项目中没有返回错误
- excel - Excel VBA Web Scraping - 通过 XML HTTP 请求忽略多个表之一
- database - 在 Oracle 11g 中创建新实例
- javascript - 包裹在 UseEffect 中的 React 异步函数
- mongodb - Mongodb如何匹配两个字段之一不等于零?