拆分自然数列表

时间:2019-11-15 11:39:42

标签: coq

我有一个自然数列表,想在列表中的每个“新”元素处将其打破。为了更清楚一点,如果我有列表[5; 5; 5; 10; 10; 15; 15],那么我要输出三个列表:[5; 5; 5][10; 10][15; 15]。我遇到的主要问题是我不知道如何在Fixpoint上输出几个元素。我认为我需要嵌套Fixpoint,但是我不知道如何嵌套。作为一个非常原始的想法,我有:

Fixpoint count (v:nat) (l:list nat) : nat :=
  match l with
  | [] => 0
  | h :: t => (if beq_nat h v then 1 else 0) + (count v t)
  end.
Fixpoint prefix (n: count)( l :list nat)=
 match l with
 | [] => []
 | m :: l' => if beq_nat count  m then []
           else m :: prefix n l'
 end.

1 个答案:

答案 0 :(得分:0)

在该示例中,我认为您不需要嵌套定点。看起来,您需要一个累加器来记住您已经收集的列表。您想要的是一个返回以下类型的函数:list (list nat)

通过prefix函数,您可以添加一个新参数(最初设置为空列表[]),该参数会记住您已经识别出的相同数字的列表:

Fixpoint prefix (acc: list (list nat)) (n: count)( l :list nat)=
  match l with
     | [] => acc
     | m :: l' => 
         (* Here some code that depending if m is equal to n will either 
            add a new list to acc or add a new element to the first list of acc *)

     end.

我没有试图更精确地回答,因为这看起来像是一项家庭作业。

我建议您尝试使用功能性编程语言(例如OCaml或Haskell)对此进行编码,而不使用命令性部分(分配等),然后再尝试在Coq中对此进行模仿(如果这对您来说更容易)

您可能还希望在线查看一些有关如何在函数式编程中使用累加器的资源。

还请注意,您的前缀代码无法编译。