Skip to content

Commit

Permalink
Add uncons' to base; rewrite head' and tail' in terms of uncons'
Browse files Browse the repository at this point in the history
  • Loading branch information
Russoul authored and gallais committed Aug 23, 2023
1 parent 694b165 commit ebbae42
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions libs/base/Data/List.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
|||
Expand Down

0 comments on commit ebbae42

Please sign in to comment.