HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulefirst-class-families-0.8.2.0Haskell2010

Fcf.Data.List

Lists.

See also Fcf.Class.Foldable for additional functions.

Contents
  1. Basic functions
  2. List transformations
  3. Reducing lists
  4. Unfolding and building
  5. Sublists
  6. Predicates
  7. Searching
  8. Indexing lists
  9. Zipping and unzipping
Synopsis

Basic functions

datadata (++) (b :: [a]) (c :: [a]) (d :: [a])

List catenation.

Example
Example2 expressions
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]
Instances1Eval
  • type Eval (xs ++ ys) = xs <> ysDefined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Head (b :: [a]) (c :: Maybe a)
#
Instances2Eval
  • type Eval (Head '[]) = 'NothingDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Head (a2 ': _as)) = 'Just a2Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Last (b :: [a]) (c :: Maybe a)
#
Instances3Eval
  • type Eval (Last '[a2]) = 'Just a2Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Last '[]) = 'NothingDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Last (a2 ': b ': as)) = Eval (Last (b ': as))Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Tail (b :: [a]) (c :: Maybe [a])
#
Instances2Eval
  • type Eval (Tail '[]) = 'NothingDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Tail (_a ': as)) = 'Just asDefined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Cons (b :: a) (c :: [a]) (d :: [a])

Append an element to a list.

Example
Example2 expressions
: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]
Instances1Eval
  • type Eval (Cons a2 as) = a2 ': asDefined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Snoc (b :: [a]) (c :: a) (d :: [a])

Append an element to the end of a list.

Example
Example1 expression
:kind! Eval (Snoc [1,2,3] 4)Eval (Snoc [1,2,3] 4) :: [Natural]= [1, 2, 3, 4]
Instances1Eval
  • type Eval (Snoc lst a) = Eval (lst ++ '[a])Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Cons2 (c :: (a, b)) (d :: ([a], [b])) (e :: ([a], [b]))

Append elements to two lists. Used in the definition of Unzip.

Instances1Eval
  • type Eval (Cons2 '(a3, b) '(as, bs)) = '(a3 ': as, b ': bs)Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Init (b :: [a]) (c :: Maybe [a])
#
Instances3Eval
  • type Eval (Init '[a2]) = 'Just '[]Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Init '[]) = 'NothingDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Init (a2 ': b ': as)) = Eval (Map (Cons a2) =<< Init (b ': as))Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Uncons (b :: [a]) (c :: Maybe (a, [a]))
#
Instances2Eval
  • type Eval (Uncons '[]) = 'NothingDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Uncons (a ': xs)) = 'Just '(a, xs)Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Unsnoc (b :: [a]) (c :: Maybe ([a], a))

Decompose a list into init and last

Example
Example1 expression
:kind! Eval (Unsnoc '[])Eval (Unsnoc '[]) :: Maybe ([a], a)= Nothing
Example1 expression
:kind! Eval (Unsnoc '[1])Eval (Unsnoc '[1]) :: Maybe ([Natural], Natural)= Just '( '[], 1)
Example1 expression
:kind! Eval (Unsnoc '[1,2,3])Eval (Unsnoc '[1,2,3]) :: Maybe ([Natural], Natural)= Just '([1, 2], 3)
Instances3Eval
  • type Eval (Unsnoc '[x]) = 'Just '('[], x)Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Unsnoc '[]) = 'NothingDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Unsnoc (x ': y ': ys)) = Eval (Map (PrependF x) =<< Unsnoc (y ': ys))Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Singleton (b :: a) (c :: [a])
#
Instances1Eval
  • type Eval (Singleton x) = '[x]Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Null (b :: [a]) (c :: Bool)
#
Instances2Eval
  • type Eval (Null '[]) = 'TrueDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Null (a2 ': as)) = 'FalseDefined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Length (b :: [a]) (c :: Nat)
#
Instances2Eval
  • type Eval (Length '[]) = 0Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Length (a2 ': as)) = 1 + Eval (Length as)Defined in first-class-families-0.8.2.0 · Fcf.Data.List

List transformations

datadata Reverse (b :: [a]) (c :: [a])

Reverse a list.

Example
Example1 expression
:kind! Eval (Reverse [1,2,3,4,5])Eval (Reverse [1,2,3,4,5]) :: [Natural]= [5, 4, 3, 2, 1]
Instances1Eval
  • type Eval (Reverse l) = Eval (Rev l '[])Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Intersperse (b :: a) (c :: [a]) (d :: [a])

Intersperse a separator between elements of a list.

Example
Example1 expression
:kind! Eval (Intersperse 0 [1,2,3,4])Eval (Intersperse 0 [1,2,3,4]) :: [Natural]= [1, 0, 2, 0, 3, 0, 4]
Instances2Eval
  • type Eval (Intersperse _1 '[]) = '[]Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Intersperse sep (x ': xs)) = x ': Eval (PrependToAll sep xs)Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Intercalate (b :: [a]) (c :: [[a]]) (d :: [a])

Join a list of words separated by some word.

Example
Example1 expression
:kind! Eval (Intercalate '[", "] [ '["Lorem"], '["ipsum"], '["dolor"] ])Eval (Intercalate '[", "] [ '["Lorem"], '["ipsum"], '["dolor"] ]) :: [TL.Symbol]= ["Lorem", ", ", "ipsum", ", ", "dolor"]
Instances1Eval

Reducing lists

datadata Foldr (c :: a -> b -> Exp b) (d :: b) (e :: t a) (f :: b)

Right fold.

Example
Example1 expression
:kind! Eval (Foldr (+) 0 [1, 2, 3, 4])Eval (Foldr (+) 0 [1, 2, 3, 4]) :: Natural= 10
Instances6Eval
  • type Eval (Foldr f y 'Nothing) = yDefined in first-class-families-0.8.2.0 · Fcf.Class.Foldable
  • type Eval (Foldr f y '[]) = yDefined in first-class-families-0.8.2.0 · Fcf.Class.Foldable
  • type Eval (Foldr f y ('Left _a)) = yDefined in first-class-families-0.8.2.0 · Fcf.Class.Foldable
  • type Eval (Foldr f y ('Right x)) = Eval (f x y)Defined in first-class-families-0.8.2.0 · Fcf.Class.Foldable
  • type Eval (Foldr f y ('Just x)) = Eval (f x y)Defined in first-class-families-0.8.2.0 · Fcf.Class.Foldable
  • type Eval (Foldr f y (x ': xs)) = Eval (f x (Eval (Foldr f y xs)))Defined in first-class-families-0.8.2.0 · Fcf.Class.Foldable
datadata UnList (c :: b) (d :: a -> b -> Exp b) (e :: [a]) (f :: b)

This is Foldr with its argument flipped.

Instances1Eval
  • type Eval (UnList y f xs) = Eval (Foldr f y xs)Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Concat (a :: t m) (b :: m)

Concatenate a collection of elements from a monoid.

Example

For example, fold a list of lists.

Concat :: [[a]] -> Exp [a]
Example2 expressions
: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]
Instances1Eval
datadata ConcatMap (c :: a -> Exp [b]) (d :: t a) (e :: [b])

Map a function and concatenate the results.

This is FoldMap specialized to the list monoid.

Instances1Eval

Unfolding and building

datadata Unfoldr (c :: b -> Exp (Maybe (a, b))) (d :: b) (e :: [a])

Unfold a generator into a list.

Example
Example2 expressions
data ToThree :: Nat -> Exp (Maybe (Nat, Nat)):{type instance Eval (ToThree b) =  If (4 TL.<=? b)    Nothing    (Just '(b, b TL.+ 1)):}
Example1 expression
:kind! Eval (Unfoldr ToThree 0)Eval (Unfoldr ToThree 0) :: [Natural]= [0, 1, 2, 3]

See also the definition of Replicate.

Instances1Eval
  • type Eval (Unfoldr f c) = Eval (UnfoldrCase f (f @@ c))Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Replicate (b :: Nat) (c :: a) (d :: [a])

Repeat the same element in a list.

Example
Example1 expression
:kind! Eval (Replicate 4 '("ok", 2))Eval (Replicate 4 '("ok", 2)) :: [(TL.Symbol, Natural)]= ['("ok", 2), '("ok", 2), '("ok", 2), '("ok", 2)]
Instances1Eval

Sublists

datadata Take (b :: Nat) (c :: [a]) (d :: [a])

Take a prefix of fixed length.

Example
Example1 expression
:kind! Eval (Take 2 [1,2,3,4,5])Eval (Take 2 [1,2,3,4,5]) :: [Natural]= [1, 2]
Instances1Eval
  • type Eval (Take n as) = Take_ n asDefined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Drop (b :: Nat) (c :: [a]) (d :: [a])

Drop a prefix of fixed length, evaluate to the remaining suffix.

Example
Example1 expression
:kind! Eval (Drop 2 [1,2,3,4,5])Eval (Drop 2 [1,2,3,4,5]) :: [Natural]= [3, 4, 5]
Instances1Eval
  • type Eval (Drop n as) = Drop_ n asDefined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata SplitAt (b :: Nat) (c :: [a]) (d :: ([a], [a]))

Return a tuple where first element is xs prefix of length n and second element is the remainder of the list.

Example
Example1 expression
:kind! Eval (SplitAt 3 '[1,2,3,4,5])Eval (SplitAt 3 '[1,2,3,4,5]) :: ([Natural], [Natural])= '([1, 2, 3], [4, 5])
Instances1Eval
datadata TakeWhile (b :: a -> Exp Bool) (c :: [a]) (d :: [a])

Take the longest prefix of elements satisfying a predicate.

Example
Example1 expression
:kind! Eval (TakeWhile ((>=) 3) [1, 2, 3, 4, 5])Eval (TakeWhile ((>=) 3) [1, 2, 3, 4, 5]) :: [Natural]= [1, 2, 3]
Instances2Eval
datadata DropWhile (b :: a -> Exp Bool) (c :: [a]) (d :: [a])

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]

Instances2Eval
datadata Span (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a]))

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
Example1 expression
: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])
Example1 expression
:kind! Eval (Span (Flip (<) 9) [1,2,3])Eval (Span (Flip (<) 9) [1,2,3]) :: ([Natural], [Natural])= '([1, 2, 3], '[])
Example1 expression
:kind! Eval (Span (Flip (<) 0) [1,2,3])Eval (Span (Flip (<) 0) [1,2,3]) :: ([Natural], [Natural])= '( '[], [1, 2, 3])
Instances1Eval
datadata Break (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a]))

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
Example1 expression
: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])
Example1 expression
:kind! Eval (Break (Flip (<) 9) [1,2,3])Eval (Break (Flip (<) 9) [1,2,3]) :: ([Natural], [Natural])= '( '[], [1, 2, 3])
Example1 expression
:kind! Eval (Break (Flip (>) 9) [1,2,3])Eval (Break (Flip (>) 9) [1,2,3]) :: ([Natural], [Natural])= '([1, 2, 3], '[])
Instances1Eval
datadata Tails (b :: [a]) (c :: [[a]])

List of suffixes of a list.

Example
Example1 expression
:kind! Eval (Tails [0,1,2,3])Eval (Tails [0,1,2,3]) :: [[Natural]]= [[0, 1, 2, 3], [1, 2, 3], [2, 3], '[3]]
Instances2Eval
  • type Eval (Tails '[]) = '[]Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Tails (a2 ': as)) = (a2 ': as) ': Eval (Tails as)Defined in first-class-families-0.8.2.0 · Fcf.Data.List

Predicates

datadata IsPrefixOf (b :: [a]) (c :: [a]) (d :: Bool)

Return True when the first list is a prefix of the second.

Example
Example1 expression
: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
Example1 expression
: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
Example1 expression
:kind! Eval ('[] `IsPrefixOf` [0,1,3,2,4,5])Eval ('[] `IsPrefixOf` [0,1,3,2,4,5]) :: Bool= True
Example1 expression
:kind! Eval ([0,1,3,2,4,5] `IsPrefixOf` '[])Eval ([0,1,3,2,4,5] `IsPrefixOf` '[]) :: Bool= False
Instances1Eval
  • type Eval (IsPrefixOf xs ys) = IsPrefixOf_ xs ysDefined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata IsSuffixOf (b :: [a]) (c :: [a]) (d :: Bool)

Return True when the first list is a suffix of the second.

Example
Example1 expression
: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
Example1 expression
: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
Example1 expression
:kind! Eval (IsSuffixOf '[] [0,1,3,2,4,5])Eval (IsSuffixOf '[] [0,1,3,2,4,5]) :: Bool= True
Example1 expression
:kind! Eval (IsSuffixOf [0,1,3,2,4,5] '[])Eval (IsSuffixOf [0,1,3,2,4,5] '[]) :: Bool= False
Instances1Eval
datadata IsInfixOf (b :: [a]) (c :: [a]) (d :: Bool)

Return True when the first list is contained within the second.

Example
Example1 expression
: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
Example1 expression
: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
Instances1Eval

Searching

datadata Elem (b :: a) (c :: [a]) (d :: Bool)

Return True if an element is in a list.

See also FindIndex.

Example
Example2 expressions
: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
Instances1Eval
datadata Find (b :: a -> Exp Bool) (c :: [a]) (d :: Maybe a)

Find Just the first element satisfying a predicate, or evaluate to Nothing if no element satisfies the predicate.

Example
Example1 expression
:kind! Eval (Find (TyEq 0) [1,2,3])Eval (Find (TyEq 0) [1,2,3]) :: Maybe Natural= Nothing
Example1 expression
:kind! Eval (Find (TyEq 0) [1,2,3,0])Eval (Find (TyEq 0) [1,2,3,0]) :: Maybe Natural= Just 0
Instances2Eval
  • type Eval (Find _p '[]) = 'NothingDefined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Find p (a2 ': as)) = Eval (If (Eval (p a2)) (Pure ('Just a2)) (Find p as))Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Filter (b :: a -> Exp Bool) (c :: [a]) (d :: [a])

Keep all elements that satisfy a predicate, remove all that don't.

Example
Example1 expression
:kind! Eval (Filter ((>) 3) [1,2,3,0])Eval (Filter ((>) 3) [1,2,3,0]) :: [Natural]= [1, 2, 0]
Instances2Eval
  • type Eval (Filter _p '[]) = '[]Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (Filter p (a2 ': as)) = Eval (If (Eval (p a2)) ('(:) a2 <$> Filter p as) (Filter p as))Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Partition (b :: a -> Exp Bool) (c :: [a]) (d :: ([a], [a]))

Split a list into one where all elements satisfy a predicate, and a second where no elements satisfy it.

Example
Example1 expression
:kind! Eval (Partition ((>=) 35) [20, 30, 40, 50])Eval (Partition ((>=) 35) [20, 30, 40, 50]) :: ([Natural],                                                [Natural])= '([20, 30], [40, 50])
Instances1Eval
  • type Eval (Partition p lst) = Eval (Foldr (PartHelp p) '('[], '[]) lst)Defined in first-class-families-0.8.2.0 · Fcf.Data.List

Indexing lists

datadata FindIndex (b :: a -> Exp Bool) (c :: [a]) (d :: Maybe Nat)

Find the index of an element satisfying the predicate.

Example
Example1 expression
:kind! Eval (FindIndex ((<=) 3) [1,2,3,1,2,3])Eval (FindIndex ((<=) 3) [1,2,3,1,2,3]) :: Maybe Natural= Just 2
Example1 expression
:kind! Eval (FindIndex ((>) 0) [1,2,3,1,2,3])Eval (FindIndex ((>) 0) [1,2,3,1,2,3]) :: Maybe Natural= Nothing
Instances2Eval
datadata SetIndex (b :: Nat) (c :: a) (d :: [a]) (e :: [a])

Modify an element at a given index.

The list is unchanged if the index is out of bounds.

Example
Example1 expression
:kind! Eval (SetIndex 2 7 [1,2,3])Eval (SetIndex 2 7 [1,2,3]) :: [Natural]= [1, 2, 7]
Instances1Eval
  • type Eval (SetIndex n a' as) = SetIndexImpl n a' asDefined in first-class-families-0.8.2.0 · Fcf.Data.List

Zipping and unzipping

datadata ZipWith (d :: a -> b -> Exp c) (e :: [a]) (f :: [b]) (g :: [c])

Combine elements of two lists pairwise.

Example
Example1 expression
:kind! Eval (ZipWith (+) [1,2,3] [1,1,1])Eval (ZipWith (+) [1,2,3] [1,1,1]) :: [Natural]= [2, 3, 4]
Instances3Eval
  • type Eval (ZipWith _f '[] _bs) = '[]Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (ZipWith _f _as '[]) = '[]Defined in first-class-families-0.8.2.0 · Fcf.Data.List
  • type Eval (ZipWith f (a2 ': as) (b2 ': bs)) = Eval (f a2 b2) ': Eval (ZipWith f as bs)Defined in first-class-families-0.8.2.0 · Fcf.Data.List
datadata Zip (c :: [a]) (d :: [b]) (e :: [(a, b)])
#
Instances1Eval
datadata Unzip (c :: Exp [(a, b)]) (d :: ([a], [b]))
#
Instances1Eval