Skip to content

Commit 7bef1a5

Browse files
Add delete, nub, sort to Data.List
1 parent 6ded3d1 commit 7bef1a5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/Haskell/Data/List.agda

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,39 @@ module Haskell.Data.List where
22

33
open import Haskell.Prelude
44

5+
open import Haskell.Data.Ord using (comparing)
6+
7+
open import Haskell.Law.Eq
8+
open import Haskell.Law.Equality
9+
510
{-----------------------------------------------------------------------------
611
Operations
712
------------------------------------------------------------------------------}
813

914
partition : (a Bool) List a (List a × List a)
1015
partition p xs = (filter p xs , filter (not ∘ p) xs)
1116

17+
deleteBy : (a a Bool) a List a List a
18+
deleteBy eq x ys = filter (not ∘ (eq x)) ys
19+
20+
delete : ⦃ Eq a ⦄ a List a List a
21+
delete = deleteBy (_==_)
22+
23+
-- | These semantics of 'nub' assume that the 'Eq' instance
24+
-- is lawful.
25+
-- These semantics are inefficient, but good for proofs.
26+
nub : ⦃ _ : Eq a ⦄ @0 ⦃ IsLawfulEq a ⦄ List a List a
27+
nub [] = []
28+
nub (x ∷ xs) = x ∷ delete x (nub xs)
29+
30+
postulate
31+
sortBy : (a a Ordering) List a List a
32+
33+
sort : ⦃ Ord a ⦄ List a List a
34+
sort = sortBy compare
35+
36+
sortOn : ⦃ Ord b ⦄ (a b) List a List a
37+
sortOn f =
38+
map snd
39+
∘ sortBy (comparing fst)
40+
∘ map (λ x let y = f x in seq y (y , x))

0 commit comments

Comments
 (0)