[Haskell-cafe] Is there any way to parametrize a value update using record syntax?

Poprádi Árpád popradi_arpad at freemail.hu
Wed Sep 7 19:23:50 CEST 2011


Hi Erik,

thanks a lot!
fclabels is an amazing package!
My code become much clearer.

Greetings,
Árpád

On Wed, 2011-09-07 at 00:04 +0200, Erik Hesselink wrote:
> 2011/9/6 Poprádi Árpád <popradi_arpad at freemail.hu>:
> > i have a record with a lot of items used in a state monad.
> >
> > data BigData = BigData {
> >                   data1 :: X
> >                 , data2 :: X
> >                -- and so on
> >                }
> >
> > updateData1 :: X -> MonadicEnv()
> > updateData1 d = do; env <- get; put env {data1 = d}
> >
> > updateData2 :: X -> MonadicEnv()
> > updateData2 d = do; env <- get; put env {data2 = d}
> >
> > But it's ugly. Always the same, only the record selector has another
> > name.
> > Is it possible to generalize it?
> 
> You can use the fclabels package [1] for this. It makes record labels
> first class, and also provides functions to update parts of a record
> in the state monad [2]. You would be able to write something like:
> 
> updateData1 = puts data1 d
> 
> It has a function for modifcation as well, which is even uglier with
> regular record syntax.
> 
> Erik
> 
> [1] http://hackage.haskell.org/package/fclabels
> [2] http://hackage.haskell.org/packages/archive/fclabels/1.0.4/doc/html/Data-Label-PureM.html#v:puts
> 






More information about the Haskell-Cafe mailing list