{- |
Description: A collection of functions that modify lists.
License: GNU LGPLv3
Maintainer: paul.bittner@uni-ulm.de
A collection of functions that modify lists.
-}
module ListUtil where
safehead :: [a] -> Maybe a
safehead :: [a] -> Maybe a
safehead [] = Maybe a
forall a. Maybe a
Nothing
safehead (x :: a
x:_) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
getRange :: Int -> Int -> [a] -> [a]
getRange :: Int -> Int -> [a] -> [a]
getRange _ _ [] = []
getRange i :: Int
i j :: Int
j l :: [a]
l = ([a]
l[a] -> Int -> a
forall a. [a] -> Int -> a
!!) (Int -> a) -> [Int] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
i..Int
j]
insertAtIndex :: Int -> a -> [a] -> [a]
insertAtIndex :: Int -> a -> [a] -> [a]
insertAtIndex i :: Int
i x :: a
x [] = [a
x]
insertAtIndex i :: Int
i x :: a
x l :: [a]
l@(head :: a
head:tail :: [a]
tail)
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
l
| Bool
otherwise = a
heada -> [a] -> [a]
forall a. a -> [a] -> [a]
:(Int -> a -> [a] -> [a]
forall a. Int -> a -> [a] -> [a]
insertAtIndex (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) a
x [a]
tail)
insertListAtIndex :: Int -> [a] -> [a] -> [a]
insertListAtIndex :: Int -> [a] -> [a] -> [a]
insertListAtIndex i :: Int
i l :: [a]
l [] = [a]
l
insertListAtIndex i :: Int
i arg :: [a]
arg l :: [a]
l@(head :: a
head:tail :: [a]
tail)
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = a
heada -> [a] -> [a]
forall a. a -> [a] -> [a]
:Int -> [a] -> [a] -> [a]
forall a. Int -> [a] -> [a] -> [a]
insertListAtIndex (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) [a]
arg [a]
tail
| Bool
otherwise = [a]
arg[a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++[a]
l
removeIndex :: Int -> [a] -> [a]
removeIndex :: Int -> [a] -> [a]
removeIndex i :: Int
i = Int -> Int -> [a] -> [a]
forall a. Int -> Int -> [a] -> [a]
removeRange Int
i Int
i
removeRange :: Int -> Int -> [a] -> [a]
removeRange :: Int -> Int -> [a] -> [a]
removeRange i :: Int
i j :: Int
j [] = []
removeRange i :: Int
i j :: Int
j (head :: a
head:tail :: [a]
tail)
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0 = [Char] -> [a]
forall a. HasCallStack => [Char] -> a
error "i < 0 in removeRange"
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
j = [Char] -> [a]
forall a. HasCallStack => [Char] -> a
error "i > j in removeRange"
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 Bool -> Bool -> Bool
&& Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = Int -> Int -> [a] -> [a]
forall a. Int -> Int -> [a] -> [a]
removeRange 0 (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) [a]
tail
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 Bool -> Bool -> Bool
&& Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = [a]
tail
| Bool
otherwise = a
heada -> [a] -> [a]
forall a. a -> [a] -> [a]
:(Int -> Int -> [a] -> [a]
forall a. Int -> Int -> [a] -> [a]
removeRange (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) [a]
tail)
removeDuplicates :: (Eq a) => [a] -> [a]
removeDuplicates :: [a] -> [a]
removeDuplicates [] = []
removeDuplicates [x :: a
x] = [a
x]
removeDuplicates (x :: a
x:xs :: [a]
xs) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [ a
k | a
k <- [a] -> [a]
forall a. Eq a => [a] -> [a]
removeDuplicates [a]
xs, a
k a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
x ]