繁体   English   中英

阿格达:返回空列表的头尾

[英]Agda: Return head and tail of empty list

我正在学习agda并在列表上练习以获得更好的理解。 现在我正在尝试为列表编写函数。 我很困惑如何返回空列表的头部和尾部。 这是我的代码:

data list (A : Set) : Set where
[]  : list A
_∷_ : A → list A → list A

Null : {A : Set} → (list A) → Bool
Null [] = true
Null (x ∷ a) = false

tail :  {A : Set} → (list A) → A
tail [] = {!!}
tail (x ∷ []) = x  
tail (x ∷ a) = tail a

head : {A : Set} → (list A) →  A
head [] = {!!}
head (x ∷ a) = x

我找到的一个解决方法是,我返回一个包含第一个和最后一个成员的列表,而不是返回第一个和最后一个成员,如下所示:

tail :  {A : Set} → (list A) → (list A)
tail [] = []
tail (x ∷ []) = x ∷ []  
tail (x ∷ a) = tail a

head : {A : Set} → (list A) → (list A)
head [] = []
head (x ∷ a) = (x ∷ [])

但我仍然对如何返回头部和尾部值感到困惑。 我怎样才能做到这一点?

PS不是作业。 在课堂上提前几英里

在Agda中,函数是总的:如果你有head : {A : Set} -> list A -> A ,那么它需要在所有列表上定义。 但是,对于head []你不能为某个任意类型A想象一个元素(想象head ([] : list Void) ...)

问题是你的head承诺太多了。 事实上,你可以返回任何列表的第一个元素; 你只能用于非空列表。 所以你需要改变head要么单独证明空虚 ,要么采用非空列表作为参数:

module SeparateProof where
  open import Data.List
  open import Data.Bool
  open import Data.Unit

  head : {A : Set} → (xs : List A) → {{nonEmpty : T (not (null xs))}} → A
  head [] {{nonEmpty = ()}} -- There's no way to pass a proof of non-emptiness for an empty list!
  head (x ∷ xs) = x

module NonEmptyType where
  open import Data.List.NonEmpty hiding (head)

  head : {A : Set} → List⁺ A → A
  head (x ∷ xs) = x -- This is the only pattern matching a List⁺ A!

这是一个用词不当,最后你不想尾巴,对于正确的尾部函数,你可以看看标准库的Data \\ List \\ Base.agda中的drop:

drop : ∀ {a} {A : Set a} → ℕ → List A → List A
drop zero    xs       = xs
drop (suc n) []       = []
drop (suc n) (x ∷ xs) = drop n xs
--e.g. drop 1
tail : ∀ {a} {A : Set a} → List A → List A
tail []       = []
tail (x ∷ xs) = xs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM