簡體   English   中英

Agda: std-lib: List: 與 snoc 的模式匹配

[英]Agda: std-lib: List: pattern matching with snoc

我寫了一個 function 從 std-lib 中獲取除了List的最后一個元素之外的所有內容:

open import Data.List

allButLast : ∀ {a} {A : Set a} → List A → List A
allButLast []       = []
allButLast (x ∷ []) = []
allButLast (x ∷ xs) = x ∷ allButLast xs

為了證明的目的,我想改用∷ʳ重寫這個 function:

allButLast2 : ∀ {a} {A : Set a} → List A → List A
allButLast2 []        = []
allButLast2 (xs ∷ʳ x) = xs

但我收到此錯誤:

Could not parse the left-hand side allButLast2 (xs ∷ʳ x)
Operators used in the grammar:
  v.∷ʳ   (infixl operator, level 5) [_∷ʳ_ (/Users/fss/Dropbox/Documents/projects/Coding/dev/agda/agda-stdlib-1.4/src/Data/Vec/Base.agda:275,1-5)] 
  ∷ʳ (infixl operator, level 6) [_∷ʳ_ (/Users/fss/Dropbox/Documents/projects/Coding/dev/agda/agda-stdlib-1.4/src/Data/List/Base.agda:351,1-5)]
when scope checking the left-hand side allButLast2 (xs ∷ʳ x) in
the definition of allButLast2

我不明白為什么它不能解析左側

allButLast2 (xs ∷ʳ x) = xs

_∷ʳ_運算符是 function,不是構造函數或模式同義詞,因此不能在模式中使用:

infixl 6 _∷ʳ_

_∷ʳ_ : List A → A → List A
xs ∷ʳ x = xs ++ [ x ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM