[Haskell-cafe] Re: is there a more concise way to generate helper functions for a datatype built on records?

Henning Thielemann lemming at henning-thielemann.de
Mon Nov 26 10:12:07 EST 2007


On Mon, 26 Nov 2007, apfelmus wrote:

> Benedikt Huber wrote:
> >  > type Upd a = a -> a
> >  > data Ref cx t
> >  > = Ref { select :: cx -> t , update :: Upd t -> Upd cx }
>
> Functional references are also called "lens", I'm going to use that term
> from now on.
>
> As a minor note, I somehow prefer a single primitive
>
>    data Lens s a = Lens { focus :: s -> (a, a -> s) }
>
>    put :: Lens s a -> a -> s -> s
>    put x = flip $ snd . focus x
>
>    get :: Lens s a -> s -> a
>    get x = fst . focus x
>
>    update :: Lens s a -> (a -> a) -> (s -> s)
>    update x f s = let (a,g) = focus x s in g (f a)


I proposed something similar on
  http://www.haskell.org/haskellwiki/Record_access


More information about the Haskell-Cafe mailing list