Add uncons' to base; rewrite head' and tail' in terms of uncons'

This commit is contained in:
russoul 2023-08-22 18:40:05 +04:00 committed by G. Allais
parent 694b1650c8
commit ebbae42c85
2 changed files with 10 additions and 4 deletions

View File

@ -191,6 +191,8 @@
* The `Data.List1` functions `foldr1` and `foldr1By` are now `public export`. * The `Data.List1` functions `foldr1` and `foldr1By` are now `public export`.
* Added `uncons' : List a -> Maybe (a, List a)` to `base`.
#### System #### System
* Changes `getNProcessors` to return the number of online processors rather than * Changes `getNProcessors` to return the number of online processors rather than

View File

@ -561,17 +561,21 @@ init [] impossible
init [x] = [] init [x] = []
init (x :: xs@(_::_)) = x :: init xs init (x :: xs@(_::_)) = x :: init xs
||| Attempt to deconstruct the list into a head and a tail.
public export
uncons' : List a -> Maybe (a, List a)
uncons' [] = Nothing
uncons' (x :: xs) = Just (x, xs)
||| Attempt to get the head of a list. If the list is empty, return `Nothing`. ||| Attempt to get the head of a list. If the list is empty, return `Nothing`.
public export public export
head' : List a -> Maybe a head' : List a -> Maybe a
head' [] = Nothing head' = map fst . uncons'
head' (x::_) = Just x
||| Attempt to get the tail of a list. If the list is empty, return `Nothing`. ||| Attempt to get the tail of a list. If the list is empty, return `Nothing`.
export export
tail' : List a -> Maybe (List a) tail' : List a -> Maybe (List a)
tail' [] = Nothing tail' = map snd . uncons'
tail' (_::xs) = Just xs
||| Attempt to retrieve the last element of a non-empty list. ||| Attempt to retrieve the last element of a non-empty list.
||| |||