Functional lenses
问题 Could someone explain functional lenses to me? It's a surprisingly difficult subject to google for and I haven't made any progress. All I know is that they provide similar get/set functionality than in OO. 回答1: A lens consists of two functions, a getter and a setter: data Lens a b = Lens { getter :: a -> b, setter :: b -> a -> a } For example, we might have lenses for the first and second parts of a pair: fstLens :: Lens (a, b) a fstLens = Lens fst $ \x (a, b) -> (x, b) sndLens :: Lens (a, b)