[Haskell-cafe] Is there any way to parametrize a value update using record syntax?
Erik Hesselink
hesselink at gmail.com
Wed Sep 7 00:04:02 CEST 2011
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