Skip to content

Commit edba5a5

Browse files
authored
[ new ] unsnoc (#1360)
1 parent 8d21832 commit edba5a5

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ Other minor additions
366366

367367
* Added infix declarations to `Data.Product.∃-syntax` and `Data.Product.∄-syntax`.
368368

369+
* Added new function to `Data.List.Base`:
370+
```agda
371+
unsnoc : List A → Maybe (List A × A)
372+
```
373+
369374
* Added new definitions to `Function.Bundles`:
370375
```agda
371376
record Func : Set _

src/Data/List/Base.agda

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ infixr 5 _ʳ++_
344344
_ʳ++_ : List A List A List A
345345
_ʳ++_ = flip reverseAcc
346346

347-
-- Snoc.
347+
-- Snoc: Cons, but from the right.
348348

349349
infixl 6 _∷ʳ_
350350

@@ -370,14 +370,18 @@ data InitLast {A : Set a} : List A → Set a where
370370
[] : InitLast []
371371
_∷ʳ′_ : (xs : List A) (x : A) InitLast (xs ∷ʳ x)
372372

373-
374-
375373
initLast : (xs : List A) InitLast xs
376374
initLast [] = []
377375
initLast (x ∷ xs) with initLast xs
378376
... | [] = [] ∷ʳ′ x
379377
... | ys ∷ʳ′ y = (x ∷ ys) ∷ʳ′ y
380378

379+
-- uncons, but from the right
380+
unsnoc : List A Maybe (List A × A)
381+
unsnoc as with initLast as
382+
... | [] = nothing
383+
... | xs ∷ʳ′ x = just (xs , x)
384+
381385
------------------------------------------------------------------------
382386
-- Splitting a list
383387

0 commit comments

Comments
 (0)