Friday, September 12, 2014

[prbwmqwj] Functions to modify a record

Haskell could use some new syntax LAMBDA_RECORD_MODIFY which could be used as follows:

import qualified Control.Monad.State as State;
data Record { field :: Int };
... State.modify $ LAMBDA_RECORD_MODIFY { field = ... };

which is equivalent to

State.modify $ \x -> x { field = ... }

but not having to name the lambda parameter "x" (twice).

I suspect this is one of the things lenses are trying to do.

2 comments :

Erik said...

That's exactly one of the use cases of lenses. The fclabels library uses it as a motivating example on its hackage page.

Luke Palmer said...

Yes, this is the motivation for lenses. This proposal doesn't work well, because often we need to do, e.g.

State.modify $ \x -> x { field = field x + 1 }

and without a named x, that is not expressible. Lenses it is.