on master help sign in
Merge pull request #1062 from johnny-john/list-add
7442132Edwin Brady
libs/prelude/Prelude/List.idr+27
@@ -12,6 +12,7 @@ import Prelude.Nat
12%access public
12%access public
13%default total
13%default total
14
14
15+infix 5 \\
15infixr 7 ::,++
16infixr 7 ::,++
16
17
17%elim data List a
18%elim data List a
@@ -399,6 +400,22 @@ nubBy = nubBy' []
399nub : Eq a => List a -> List a
400nub : Eq a => List a -> List a
400nub = nubBy (==)
401nub = nubBy (==)
401
402
403+deleteBy : (a -> a -> Bool) -> a -> List a -> List a
404+deleteBy _ _ [] = []
405+deleteBy eq x (y::ys) = if x `eq` y then ys else y :: deleteBy eq x ys
406+
407+delete : (Eq a) => a -> List a -> List a
408+delete = deleteBy (==)
409+
410+(\\) : (Eq a) => List a -> List a -> List a
411+(\\) = foldl (flip delete)
412+
413+unionBy : (a -> a -> Bool) -> List a -> List a -> List a
414+unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
415+
416+union : (Eq a) => List a -> List a -> List a
417+union = unionBy (==)
418+
402--------------------------------------------------------------------------------
419--------------------------------------------------------------------------------
403-- Splitting and breaking lists
420-- Splitting and breaking lists
404--------------------------------------------------------------------------------
421--------------------------------------------------------------------------------
@@ -439,6 +456,16 @@ partition p (x::xs) =
439 else
456 else
440 (lefts, x::rights)
457 (lefts, x::rights)
441
458
459+inits : List a -> List (List a)
460+inits xs = [] :: case xs of
461+ [] => []
462+ x :: xs' => map (x ::) (inits xs')
463+
464+tails : List a -> List (List a)
465+tails xs = xs :: case xs of
466+ [] => []
467+ _ :: xs' => tails xs'
468+
442--------------------------------------------------------------------------------
469--------------------------------------------------------------------------------
443-- Predicates
470-- Predicates
444--------------------------------------------------------------------------------
471--------------------------------------------------------------------------------