不使用辅助函数的定义函数

Definition function without using a helper function

我一直在阅读软件基础并解决其中的问题。这是我要定义的定义之一:

Fixpoint split {X Y : Type} (l : list (X*Y)) : (list X) * (list Y)

基本上它是 haskell 的 unzip 版本。

我是这样实现的:

Fixpoint split2 {X Y : Type} (l : list (X*Y)) (g :(list X) * (list Y))
               : (list X) * (list Y) :=
  match l with
    | [] => g
    | (x,y)::xs => split2 xs ((fst g) ++ [x],(snd g) ++ [y])
  end.

Fixpoint split {X Y : Type} (l : list (X*Y))
               : (list X) * (list Y) :=
split2 l ([],[]).

我有两个问题:

Coq 中有 let。您可以而且应该只翻译标准 Haskell 定义,避免 ++:

的二次性能
Fixpoint split {A B} (l : list (A * B)) : list A * list B :=
  match l with
    [] => ([], [])
  | (x, y) :: xs => let (xs2, ys2) := split xs in (x::xs2, y::ys2)
  end.