Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Lists.
See also Fcf.Class.Foldable for additional functions.
Synopsis
- data ((b :: [a]) ++ (c :: [a])) (d :: [a])
- data Head (b :: [a]) (c :: Maybe a)
- data Last (b :: [a]) (c :: Maybe a)
- data Tail (b :: [a]) (c :: Maybe [a])
- data Cons (b :: a) (c :: [a]) (d :: [a])
- data Snoc (b :: [a]) (c :: a) (d :: [a])
- data Cons2 (c :: (a, b)) (d :: ([a], [b])) (e :: ([a], [b]))
- data Init (b :: [a]) (c :: Maybe [a])
- data Null (b :: [a]) (c :: Bool)
- data Length (b :: [a]) (c :: Nat)
- data Reverse (b :: [a]) (c :: [a])
- data Intersperse (b :: a) (c :: [a]) (d :: [a])
- data Intercalate (b :: [a]) (c :: [[a]]) (d :: [a])
- data Foldr (c :: a -> b -> Exp b) (d :: b) (e :: t a) (f :: b)
- data UnList (c :: b) (d :: a -> b -> Exp b) (e :: [a]) (f :: b)
- data Concat (a :: t m) (b :: m)
- data ConcatMap (c :: a -> Exp [b]) (d :: t a) (e :: [b])
- data Unfoldr (c :: b -> Exp (Maybe (a, b))) (d :: b) (e :: [a])
- data Replicate (b :: Nat) (c :: a) (d :: [a])
- data Take (b :: Nat) (c :: [a]) (d :: [a])
- data Drop (b :: Nat) (c :: [a]) (d :: [a])
- data TakeWhile (b :: a -> Exp Bool) (c :: [a]) (d :: [a])
- data DropWhile (b :: a -> Exp Bool) (c :: [a]) (d :: [a])
- data Span (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a]))
- data Break (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a]))
- data Tails (b :: [a]) (c :: [[a]])
- data IsPrefixOf (b :: [a]) (c :: [a]) (d :: Bool)
- data IsSuffixOf (b :: [a]) (c :: [a]) (d :: Bool)
- data IsInfixOf (b :: [a]) (c :: [a]) (d :: Bool)
- data Elem (b :: a) (c :: [a]) (d :: Bool)
- data Lookup (a :: k) (c :: [(k, b)]) (d :: Maybe b)
- data Find (b :: a -> Exp Bool) (c :: [a]) (d :: Maybe a)
- data Filter (b :: a -> Exp Bool) (c :: [a]) (d :: [a])
- data Partition (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a]))
- data FindIndex (b :: a -> Exp Bool) (c :: [a]) (d :: Maybe Nat)
- data SetIndex (b :: Nat) (c :: a) (d :: [a]) (e :: [a])
- data ZipWith (d :: a -> b -> Exp c) (e :: [a]) (f :: [b]) (g :: [c])
- data Zip (c :: [a]) (d :: [b]) (e :: [(a, b)])
- data Unzip (c :: Exp [(a, b)]) (d :: ([a], [b]))
Basic functions
data ((b :: [a]) ++ (c :: [a])) (d :: [a]) Source #
List catenation.
Example
>>>
data Example where Ex :: a -> Example -- Hide the type of examples to avoid brittleness in different GHC versions
>>>
:kind! Ex (Eval ([1, 2] ++ [3, 4]) :: [Natural])
Ex (Eval ([1, 2] ++ [3, 4]) :: [Natural]) :: Example = Ex [1, 2, 3, 4]
data Cons (b :: a) (c :: [a]) (d :: [a]) Source #
Append an element to a list.
Example
>>>
:kind! Eval (Cons 1 [2, 3])
Eval (Cons 1 [2, 3]) :: [Natural] = [1, 2, 3]>>>
:kind! Eval (Cons Int [Char, Maybe Double])
Eval (Cons Int [Char, Maybe Double]) :: [*] = [Int, Char, Maybe Double]
data Snoc (b :: [a]) (c :: a) (d :: [a]) Source #
Append an element to the end of a list.
Example
>>>
:kind! Eval (Snoc [1,2,3] 4)
Eval (Snoc [1,2,3] 4) :: [Natural] = [1, 2, 3, 4]
data Cons2 (c :: (a, b)) (d :: ([a], [b])) (e :: ([a], [b])) Source #
Append elements to two lists. Used in the definition of Unzip
.
List transformations
data Reverse (b :: [a]) (c :: [a]) Source #
Reverse a list.
Example
>>>
:kind! Eval (Reverse [1,2,3,4,5])
Eval (Reverse [1,2,3,4,5]) :: [Natural] = [5, 4, 3, 2, 1]
data Intersperse (b :: a) (c :: [a]) (d :: [a]) Source #
Intersperse a separator between elements of a list.
Example
>>>
:kind! Eval (Intersperse 0 [1,2,3,4])
Eval (Intersperse 0 [1,2,3,4]) :: [Natural] = [1, 0, 2, 0, 3, 0, 4]
Instances
type Eval (Intersperse _1 ('[] :: [a]) :: [a] -> Type) Source # | |
Defined in Fcf.Data.List | |
type Eval (Intersperse sep (x ': xs) :: [a] -> Type) Source # | |
Defined in Fcf.Data.List |
data Intercalate (b :: [a]) (c :: [[a]]) (d :: [a]) Source #
Join a list of words separated by some word.
Example
>>>
:kind! Eval (Intercalate '[", "] [ '["Lorem"], '["ipsum"], '["dolor"] ])
Eval (Intercalate '[", "] [ '["Lorem"], '["ipsum"], '["dolor"] ]) :: [TL.Symbol] = ["Lorem", ", ", "ipsum", ", ", "dolor"]
Instances
type Eval (Intercalate xs xss :: [a] -> Type) Source # | |
Defined in Fcf.Data.List type Eval (Intercalate xs xss :: [a] -> Type) = Eval ((Concat :: [[a]] -> [a] -> Type) =<< Intersperse xs xss) |
Reducing lists
See also Fcf.Class.Foldable.
data Foldr (c :: a -> b -> Exp b) (d :: b) (e :: t a) (f :: b) Source #
Right fold.
Example
>>>
:kind! Eval (Foldr (+) 0 [1, 2, 3, 4])
Eval (Foldr (+) 0 [1, 2, 3, 4]) :: Natural = 10
Instances
type Eval (Foldr f y ('Left _a :: Either a3 a1) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('Right x :: Either a3 a1) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('Just x) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('Nothing :: Maybe a1) :: a2 -> Type) Source # | |
type Eval (Foldr f y (x ': xs) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('[] :: [a1]) :: a2 -> Type) Source # | |
Defined in Fcf.Class.Foldable |
data UnList (c :: b) (d :: a -> b -> Exp b) (e :: [a]) (f :: b) Source #
This is Foldr
with its argument flipped.
data Concat (a :: t m) (b :: m) Source #
Concatenate a collection of elements from a monoid.
Example
For example, fold a list of lists.
Concat :: [[a]] -> Exp [a]
>>>
:kind! Eval (Concat ([[1,2], [3,4], [5,6]]))
Eval (Concat ([[1,2], [3,4], [5,6]])) :: [Natural] = [1, 2, 3, 4, 5, 6]>>>
:kind! Eval (Concat ([[Int, Maybe Int], [Maybe String, Either Double Int]]))
Eval (Concat ([[Int, Maybe Int], [Maybe String, Either Double Int]])) :: [*] = [Int, Maybe Int, Maybe [Char], Either Double Int]
data ConcatMap (c :: a -> Exp [b]) (d :: t a) (e :: [b]) Source #
Map a function and concatenate the results.
This is FoldMap
specialized to the list monoid.
Unfolding and building
data Unfoldr (c :: b -> Exp (Maybe (a, b))) (d :: b) (e :: [a]) Source #
Unfold a generator into a list.
Example
>>>
data ToThree :: Nat -> Exp (Maybe (Nat, Nat))
>>>
:{
type instance Eval (ToThree b) = If (4 TL.<=? b) Nothing (Just '(b, b TL.+ 1)) :}
>>>
:kind! Eval (Unfoldr ToThree 0)
Eval (Unfoldr ToThree 0) :: [Natural] = [0, 1, 2, 3]
See also the definition of Replicate
.
data Replicate (b :: Nat) (c :: a) (d :: [a]) Source #
Repeat the same element in a list.
Example
>>>
:kind! Eval (Replicate 4 '("ok", 2))
Eval (Replicate 4 '("ok", 2)) :: [(TL.Symbol, Natural)] = ['("ok", 2), '("ok", 2), '("ok", 2), '("ok", 2)]
Sublists
data Take (b :: Nat) (c :: [a]) (d :: [a]) Source #
Take a prefix of fixed length.
Example
>>>
:kind! Eval (Take 2 [1,2,3,4,5])
Eval (Take 2 [1,2,3,4,5]) :: [Natural] = [1, 2]
data Drop (b :: Nat) (c :: [a]) (d :: [a]) Source #
Drop a prefix of fixed length, evaluate to the remaining suffix.
Example
>>>
:kind! Eval (Drop 2 [1,2,3,4,5])
Eval (Drop 2 [1,2,3,4,5]) :: [Natural] = [3, 4, 5]
data TakeWhile (b :: a -> Exp Bool) (c :: [a]) (d :: [a]) Source #
Take the longest prefix of elements satisfying a predicate.
Example
>>>
:kind! Eval (TakeWhile ((>=) 3) [1, 2, 3, 4, 5])
Eval (TakeWhile ((>=) 3) [1, 2, 3, 4, 5]) :: [Natural] = [1, 2, 3]
data DropWhile (b :: a -> Exp Bool) (c :: [a]) (d :: [a]) Source #
Drop the longest prefix of elements satisfying a predicate, evaluate to the remaining suffix.
Example
:kind! Eval (DropWhile ((>=) 3) [1, 2, 3, 4, 5]) Eval (DropWhile ((>=) 3) [1, 2, 3, 4, 5]) :: [Natural] = [4, 5]
data Span (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a])) Source #
Span
, applied to a predicate p
and a list xs
, returns a tuple:
the first component is the longest prefix (possibly empty) of xs
whose elements
satisfy p
;
the second component is the remainder of the list.
See also TakeWhile
, DropWhile
, and Break
.
Example
>>>
:kind! Eval (Span (Flip (<) 3) [1,2,3,4,1,2])
Eval (Span (Flip (<) 3) [1,2,3,4,1,2]) :: ([Natural], [Natural]) = '([1, 2], [3, 4, 1, 2])
>>>
:kind! Eval (Span (Flip (<) 9) [1,2,3])
Eval (Span (Flip (<) 9) [1,2,3]) :: ([Natural], [Natural]) = '([1, 2, 3], '[])
>>>
:kind! Eval (Span (Flip (<) 0) [1,2,3])
Eval (Span (Flip (<) 0) [1,2,3]) :: ([Natural], [Natural]) = '( '[], [1, 2, 3])
data Break (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a])) Source #
Break
, applied to a predicate p
and a list xs
, returns a tuple:
the first component is the longest prefix (possibly empty) of xs
whose elements
do not satisfy p
; the second component is the remainder of the list.
Example
>>>
:kind! Eval (Break (Flip (>) 3) [1,2,3,4,1,2])
Eval (Break (Flip (>) 3) [1,2,3,4,1,2]) :: ([Natural], [Natural]) = '([1, 2, 3], [4, 1, 2])
>>>
:kind! Eval (Break (Flip (<) 9) [1,2,3])
Eval (Break (Flip (<) 9) [1,2,3]) :: ([Natural], [Natural]) = '( '[], [1, 2, 3])
>>>
:kind! Eval (Break (Flip (>) 9) [1,2,3])
Eval (Break (Flip (>) 9) [1,2,3]) :: ([Natural], [Natural]) = '([1, 2, 3], '[])
data Tails (b :: [a]) (c :: [[a]]) Source #
List of suffixes of a list.
Example
>>>
:kind! Eval (Tails [0,1,2,3])
Eval (Tails [0,1,2,3]) :: [[Natural]] = [[0, 1, 2, 3], [1, 2, 3], [2, 3], '[3]]
Predicates
data IsPrefixOf (b :: [a]) (c :: [a]) (d :: Bool) Source #
Return True
when the first list is a prefix of the second.
Example
>>>
:kind! Eval ([0,1,2] `IsPrefixOf` [0,1,2,3,4,5])
Eval ([0,1,2] `IsPrefixOf` [0,1,2,3,4,5]) :: Bool = True
>>>
:kind! Eval ([0,1,2] `IsPrefixOf` [0,1,3,2,4,5])
Eval ([0,1,2] `IsPrefixOf` [0,1,3,2,4,5]) :: Bool = False
>>>
:kind! Eval ('[] `IsPrefixOf` [0,1,3,2,4,5])
Eval ('[] `IsPrefixOf` [0,1,3,2,4,5]) :: Bool = True
>>>
:kind! Eval ([0,1,3,2,4,5] `IsPrefixOf` '[])
Eval ([0,1,3,2,4,5] `IsPrefixOf` '[]) :: Bool = False
Instances
type Eval (IsPrefixOf xs ys :: Bool -> Type) Source # | |
Defined in Fcf.Data.List |
data IsSuffixOf (b :: [a]) (c :: [a]) (d :: Bool) Source #
Return True
when the first list is a suffix of the second.
Example
>>>
:kind! Eval (IsSuffixOf [3,4,5] [0,1,2,3,4,5])
Eval (IsSuffixOf [3,4,5] [0,1,2,3,4,5]) :: Bool = True
>>>
:kind! Eval (IsSuffixOf [3,4,5] [0,1,3,2,4,5])
Eval (IsSuffixOf [3,4,5] [0,1,3,2,4,5]) :: Bool = False
>>>
:kind! Eval (IsSuffixOf '[] [0,1,3,2,4,5])
Eval (IsSuffixOf '[] [0,1,3,2,4,5]) :: Bool = True
>>>
:kind! Eval (IsSuffixOf [0,1,3,2,4,5] '[])
Eval (IsSuffixOf [0,1,3,2,4,5] '[]) :: Bool = False
Instances
type Eval (IsSuffixOf xs ys :: Bool -> Type) Source # | |
Defined in Fcf.Data.List |
data IsInfixOf (b :: [a]) (c :: [a]) (d :: Bool) Source #
Return True
when the first list is contained within the second.
Example
>>>
:kind! Eval (IsInfixOf [2,3,4] [0,1,2,3,4,5,6])
Eval (IsInfixOf [2,3,4] [0,1,2,3,4,5,6]) :: Bool = True
>>>
:kind! Eval (IsInfixOf [2,4,4] [0,1,2,3,4,5,6])
Eval (IsInfixOf [2,4,4] [0,1,2,3,4,5,6]) :: Bool = False
Searching
data Elem (b :: a) (c :: [a]) (d :: Bool) Source #
Return True
if an element is in a list.
See also FindIndex
.
Example
>>>
:kind! Eval (Elem 1 [1,2,3])
Eval (Elem 1 [1,2,3]) :: Bool = True>>>
:kind! Eval (Elem 1 [2,3])
Eval (Elem 1 [2,3]) :: Bool = False
data Lookup (a :: k) (c :: [(k, b)]) (d :: Maybe b) Source #
Find an element associated with a key in an association list.
data Find (b :: a -> Exp Bool) (c :: [a]) (d :: Maybe a) Source #
Find Just
the first element satisfying a predicate, or evaluate to
Nothing
if no element satisfies the predicate.
Example
>>>
:kind! Eval (Find (TyEq 0) [1,2,3])
Eval (Find (TyEq 0) [1,2,3]) :: Maybe Natural = Nothing
>>>
:kind! Eval (Find (TyEq 0) [1,2,3,0])
Eval (Find (TyEq 0) [1,2,3,0]) :: Maybe Natural = Just 0
data Filter (b :: a -> Exp Bool) (c :: [a]) (d :: [a]) Source #
Keep all elements that satisfy a predicate, remove all that don't.
Example
>>>
:kind! Eval (Filter ((>) 3) [1,2,3,0])
Eval (Filter ((>) 3) [1,2,3,0]) :: [Natural] = [1, 2, 0]
data Partition (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a])) Source #
Split a list into one where all elements satisfy a predicate, and a second where no elements satisfy it.
Example
>>>
:kind! Eval (Partition ((>=) 35) [20, 30, 40, 50])
Eval (Partition ((>=) 35) [20, 30, 40, 50]) :: ([Natural], [Natural]) = '([20, 30], [40, 50])
Indexing lists
data FindIndex (b :: a -> Exp Bool) (c :: [a]) (d :: Maybe Nat) Source #
Find the index of an element satisfying the predicate.
Example
>>>
:kind! Eval (FindIndex ((<=) 3) [1,2,3,1,2,3])
Eval (FindIndex ((<=) 3) [1,2,3,1,2,3]) :: Maybe Natural = Just 2
>>>
:kind! Eval (FindIndex ((>) 0) [1,2,3,1,2,3])
Eval (FindIndex ((>) 0) [1,2,3,1,2,3]) :: Maybe Natural = Nothing
data SetIndex (b :: Nat) (c :: a) (d :: [a]) (e :: [a]) Source #
Modify an element at a given index.
The list is unchanged if the index is out of bounds.
Example
>>>
:kind! Eval (SetIndex 2 7 [1,2,3])
Eval (SetIndex 2 7 [1,2,3]) :: [Natural] = [1, 2, 7]
Zipping and unzipping
data ZipWith (d :: a -> b -> Exp c) (e :: [a]) (f :: [b]) (g :: [c]) Source #
Combine elements of two lists pairwise.
Example
>>>
:kind! Eval (ZipWith (+) [1,2,3] [1,1,1])
Eval (ZipWith (+) [1,2,3] [1,1,1]) :: [Natural] = [2, 3, 4]