[Haskell-cafe] Field updates in a state monad
Stuart Cook
scook0 at gmail.com
Thu Jan 10 19:26:52 EST 2008
On Jan 11, 2008 2:01 AM, Michael Roth <mroth at nessie.de> wrote:
> Hello list,
> Exists there a way to write this cleaner without writing countless
> "set_xyz" helper functions?
The "set_xyz" methods have to be written, but that doesn't mean *you*
have to write them. You can use Template Haskell to automatically
derive setter functions for you.
Writing the TH derivations is a little fiddly, but luckily others have
already encountered the same problem. These blog posts can point you
to a few implementations:
http://luqui.org/blog/archives/2007/08/05/haskell-state-accessors-second-attempt-composability/
(http://tinyurl.com/2ve2zw)
http://twan.home.fmf.nl/blog/haskell/overloading-functional-references.details
(http://tinyurl.com/2ustba)
In particular, take a look at Luke's code, because it also contains
helper functions for combining these getters and setters with
MonadState.
Stuart
More information about the Haskell-Cafe
mailing list