HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulelens-5.3.5Haskell2010

Data.List.Lens

Traversals for manipulating parts of a list.

Additional optics for manipulating lists are present more generically in this package.

The Ixed class allows traversing the element at a specific list index.

Example1 expression
[0..10] ^? ix 4Just 4
Example1 expression
[0..5] & ix 4 .~ 2[0,1,2,3,2,5]
Example1 expression
[0..10] ^? ix 14Nothing
Example1 expression
[0..5] & ix 14 .~ 2[0,1,2,3,4,5]

The Cons and AsEmpty classes provide Control.Lens.Prism.Prisms for list constructors.

Example1 expression
[1..10] ^? _ConsJust (1,[2,3,4,5,6,7,8,9,10])
Example1 expression
[] ^? _ConsNothing
Example1 expression
[] ^? _EmptyJust ()
Example1 expression
_Cons # (1, _Empty # ()) :: [Int][1]

Additionally, Snoc provides a Control.Lens.Prism.Prism for accessing the end of a list. Note that this Control.Lens.Prism.Prism always will need to traverse the whole list.

Example1 expression
[1..5] ^? _SnocJust ([1,2,3,4],5)
Example1 expression
_Snoc # ([1,2],5)[1,2,5]

An instance of Plated allows for finding locations in the list where a traversal matches.

Example1 expression
[Nothing, Just 7, Just 3, Nothing] & deep (ix 0 . _Just) +~ 10[Nothing,Just 17,Just 3,Nothing]

An instance of Control.Lens.Iso.Reversing provides an Control.Lens.Iso.Iso between a list and its reverse.

Example1 expression
"live" & reversed %~ ('d':)"lived"

It's possible to work under a prefix or suffix of a list using Prefixed and Suffixed.

Example1 expression
"preview" ^? prefixed "pre"Just "view"
Example1 expression
suffixed ".o" # "hello""hello.o"

At present, Data.List.Lens re-exports Prefixed and Suffixed for backwards compatibility, as prefixed and suffixed used to be top-level functions defined in this module. This may change in a future major release of lens.

Finally, it's possible to traverse, fold over, and map over index-value pairs thanks to instances of TraversableWithIndex, FoldableWithIndex, and FunctorWithIndex.

Example1 expression
imap (,) "Hello"[(0,'H'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]
Example1 expression
ifoldMap replicate "Hello""ellllloooo"
Example1 expression
itraverse_ (curry print) "Hello"(0,'H')(1,'e')(2,'l')(3,'l')(4,'o')
  • 2 classes
  • 1 value
  • Packagelens-5.3.5
  • Exports3
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceLens.hs
classclass Prefixed t where
#

Methods

  • prefixed :: t -> Prism' t t

    A Prism stripping a prefix from a sequence when used as a Traversal, or prepending that prefix when run backwards:

    Example1 expression
    "preview" ^? prefixed "pre"Just "view"
    Example1 expression
    "review" ^? prefixed "pre"Nothing
    Example1 expression
    prefixed "pre" # "amble""preamble"
Instances5Prefixed
classclass Suffixed t where
#

Methods

  • suffixed :: t -> Prism' t t

    A Prism stripping a suffix from a sequence when used as a Traversal, or appending that suffix when run backwards:

    Example1 expression
    "review" ^? suffixed "view"Just "re"
    Example1 expression
    "review" ^? suffixed "tire"Nothing
    Example1 expression
    suffixed ".o" # "hello""hello.o"
Instances5Suffixed
valuestripSuffix :: Eq a => [a] -> [a] -> Maybe [a]
#

\mathcal{O}(\min(m,n)). The stripSuffix function drops the given suffix from a list. It returns Nothing if the list did not end with the suffix given, or Just the list after the suffix, if it does.

Example1 expression
stripSuffix "bar" "foobar"Just "foo"
Example1 expression
stripSuffix "foo" "foo"Just ""
Example1 expression
stripSuffix "bar" "barfoo"Nothing
Example1 expression
stripSuffix "foo" "barfoobaz"Nothing