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`.
* Added `uncons' : List a -> Maybe (a, List a)` to `base`.
#### System
* Changes `getNProcessors` to return the number of online processors rather than

View File

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