Lenses express a high-level pattern for composition and in that sense belong firmly in Chapter 3, Patterns for Composition. However, the concept of
Lens is also deeply entwined with
Traversable, and so we describe it in this chapter instead.
Lenses relate to the getter and setter functions, which also describe access to parts of data structures. To find our way to the Lens abstraction (as per Edward Kmett's Lens library), we'll start by writing a getter and setter to access the root node of a tree.
data Tree a = Node a (Tree a) (Tree a) | Leaf a deriving Show intTree = Node 2 (Leaf 3) (Node 5 (Leaf 7) (Leaf 11)) listTree = Node [1 ,1 ] (Leaf [2,1 ]) (Node [3,2] (Leaf [5,2]) (Leaf [7,4])) tupleTree = Node (1 ,1 ) (Leaf (2,1 )) (Node (3,2) (Leaf (5,2))...