I am learning agda and practicing on lists to get a better understanding. Right now I am trying to write functions for list. I am confused on how to return the head and tail of an empty list. Here is my code:
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
A work around that I found was that instead of returning the first and last members I return a list containing the first and last members which is as follows:
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 ∷ [])
But I am still confused about how to return the head and tail values. How can I do this?
P.S Not an assignment. Miles ahead of this stuff in class
A -> B
function returns aB
for eachA
, and it never loops, never throws exception, never does anything other than returning aB
. If you find that a type doesn't have an implementation, you must change the type. In this case you may havehead : {A : Set} -> list A -> Maybe A
, or useVec
fromData.Vec
which supportshead
andtail
for nonempty vectors. – András Kovács