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

Moduleoptics-core-0.4.1.1Haskell2010

Data.List.Optics

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 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 Prism for accessing the end of a list. Note that this 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]

Finally, it's possible to traverse, fold over, and map over index-value pairs thanks to instances of Optics.Indexed.Core.TraversableWithIndex, Optics.Indexed.Core.FoldableWithIndex, and Optics.Indexed.Core.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 values
valueprefixed :: Eq a => [a] -> Prism' [a] [a]
#

A Prism stripping a prefix from a list 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"
valuesuffixed :: Eq a => [a] -> Prism' [a] [a]
#

A Prism stripping a suffix from a list 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"