xs !! n returns the element of the stream xs at index
n. Note that the head of the stream has index 0.
Beware: a negative or out-of-bounds index will cause an error.
:: a typeCtrl KGHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11
Moduleghc-lib-parser-9.14.1.20251220GHC2021
xs !! n returns the element of the stream xs at index
n. Note that the head of the stream has index 0.
Beware: a negative or out-of-bounds index will cause an error.
Prepend an element to the stream.
Attach a list at the end of a NonEmpty.
appendList (1 :| [2,3]) []1 :| [2,3]
appendList (1 :| [2,3]) [4,5]1 :| [2,3,4,5]
Use compareLength xs n as a safer and faster alternative
to compare (length xs) n. Similarly, it's better
to write compareLength xs 10 == LT instead of length xs < 10.
While length would force and traverse
the entire spine of xs (which could even diverge if xs is infinite),
compareLength traverses at most n elements to determine its result.
compareLength ('a' :| []) 1EQcompareLength ('a' :| ['b']) 3LTcompareLength (0 :| [1..]) 100GTcompareLength undefined 0GTcompareLength ('a' :| 'b' : undefined) 1GT
Synonym for <|.
cycle xs returns the infinite repetition of xs:
cycle (1 :| [2,3]) = 1 :| [2,3,1,2,3,...]drop n xs drops the first n elements off the front of
the sequence xs.
filter p xs removes any elements from xs that do not satisfy p.
Converts a normal list to a NonEmpty stream.
Raises an error if given an empty list.
The group function takes a stream and returns a list of streams such that flattening the resulting list is equal to the argument. Moreover, each stream in the resulting list contains only equal elements, and consecutive equal elements of the input end up in the same stream of the output list. For example, in list notation:
group "Mississippi"['M' :| "",'i' :| "",'s' :| "s",'i' :| "",'s' :| "s",'i' :| "",'p' :| "p",'i' :| ""]
groupAllWith operates like groupWith, but sorts the list first so that each equivalence class has, at most, one list in the output
groupAllWith1 is to groupWith1 as groupAllWith is to groupWith
groupWith1 is to group1 as groupWith is to group
Extract the first element of the stream.
Extract everything except the last element of the stream.
insert x xs inserts x into the last position in xs where it
is still less than or equal to the next element. In particular, if the
list is sorted beforehand, the result will also be sorted.
'intersperse x xs' alternates elements of the list with copies of x.
intersperse 0 (1 :| [2,3]) == 1 :| [0,2,0,3]The isPrefixOf function returns True if the first argument is a prefix of the second.
iterate f x produces the infinite sequence
of repeated applications of f to x.
iterate f x = x :| [f x, f (f x), ..]Extract the last element of the stream.
Number of elements in NonEmpty list.
The partition function takes a predicate p and a stream
xs, and returns a pair of lists. The first list corresponds to the
elements of xs for which p holds; the second corresponds to the
elements of xs for which p does not hold.
'partition' p xs = ('filter' p xs, 'filter' (not . p) xs)The permutations function returns the list of all permutations of the argument.
permutations1 operates like permutations, but uses the knowledge that its input is non-empty to produce output where every element is non-empty.
permutations1 = fmap fromList . permutations . toListAttach a list at the beginning of a NonEmpty.
prependList [] (1 :| [2,3])1 :| [2,3]
prependList [negate 1, 0] (1 :| [2, 3])-1 :| [0,1,2,3]
repeat x returns a constant stream, where all elements are
equal to x.
reverse a finite NonEmpty stream.
Construct a NonEmpty list from a single element.
some1 x sequences x one or more times.
Sort a stream.
Sort a NonEmpty on a user-supplied projection of its elements. See sortOn for more detailed information.
sortOn fst $ (2, "world") :| [(4, "!"), (1, "Hello")](1,"Hello") :| [(2,"world"),(4,"!")]
sortOn List.length ("jim" :| ["creed", "pam", "michael", "dwight", "kevin"])"jim" :| ["pam","creed","kevin","dwight","michael"]
This function minimises the projections performed, by materialising the projections in an intermediate list.
For trivial projections, you should prefer using sortBy with comparing, for example:
sortBy (comparing fst) $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]
Or, for the exact same API as sortOn, you can use `sortBy . comparing`:
(sortBy . comparing) fst $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]
sortWith is an alias for `sortBy . comparing`.
span p xs returns the longest prefix of xs that satisfies
p, together with the remainder of the stream.
'span' p xs == ('takeWhile' p xs, 'dropWhile' p xs)
xs == ys ++ zs where (ys, zs) = 'span' p xssplitAt n xs returns a pair consisting of the prefix of xs
of length n and the remaining stream immediately following this prefix.
'splitAt' n xs == ('take' n xs, 'drop' n xs)
xs == ys ++ zs where (ys, zs) = 'splitAt' n xsExtract the possibly-empty tail of the stream.
take n xs returns the first n elements of xs.
takeWhile p xs returns the longest prefix of the stream
xs for which the predicate p holds.
uncons produces the first element of the stream, and a stream of the remaining elements, if any.
Compute n-ary logic exclusive OR operation on NonEmpty list.
The zip function takes two streams and returns a stream of corresponding pairs.
Map a function over a NonEmpty stream.
Non-empty (and non-strict) list type.
a :| [a]infixr 5Monad NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanFunctor NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanMonadFix NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixApplicative NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanFoldable NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.FoldableTraversable NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.TraversableMonadZip NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.ZipFoldable1 NonEmptyDefined in base-4.22.0.0 · Data.Foldable1Eq1 NonEmptyDefined in base-4.22.0.0 · Data.Functor.ClassesOrd1 NonEmptyDefined in base-4.22.0.0 · Data.Functor.ClassesRead1 NonEmptyDefined in base-4.22.0.0 · Data.Functor.ClassesShow1 NonEmptyDefined in base-4.22.0.0 · Data.Functor.ClassesNFData1 NonEmptyDefined in deepseq-1.5.1.0 · Control.DeepSeqGeneric1 NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.GenericsLift a => Lift (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.TH.LiftIsList (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.IsListEq a => Eq (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseData a => Data (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.DataOrd a => Ord (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseRead a => Read (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.ReadShow a => Show (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.ShowGeneric (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsSemigroup (NonEmpty a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanNFData a => NFData (NonEmpty a)Defined in deepseq-1.5.1.0 · Control.DeepSeqBinary a => Binary (NonEmpty a)Defined in binary-0.8.9.3 · Data.Binary.ClassOutputable a => Outputable (NonEmpty a)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Utils.OutputableBinary a => Binary (NonEmpty a)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Utils.Binarytype Rep (NonEmpty a) = D1 ('MetaData "NonEmpty"
"GHC.Internal.Base"
"ghc-internal"
'False) (C1 ('MetaCons ":|"
('InfixI 'RightAssociative 5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [a])))Defined in ghc-internal-9.1401.0 · GHC.Internal.Genericstype Rep1 NonEmpty = D1 ('MetaData "NonEmpty"
"GHC.Internal.Base"
"ghc-internal"
'False) (C1 ('MetaCons ":|"
('InfixI 'RightAssociative 5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec1 [])))Defined in ghc-internal-9.1401.0 · GHC.Internal.Genericstype Item (NonEmpty a) = aDefined in ghc-internal-9.1401.0 · GHC.Internal.IsListList of elements of a structure, from left to right. If the entire list is intended to be reduced via a fold, just fold the structure directly bypassing the list.
Basic usage:
toList Nothing[]
toList (Just 42)[42]
toList (Left "foo")[]
toList (Node (Leaf 5) 17 (Node Empty 12 (Leaf 8)))[5,17,12,8]
For lists, toList is the identity:
toList [1, 2, 3][1,2,3]