[Haskell-cafe] Field updates in a state monad

Henning Thielemann lemming at henning-thielemann.de
Fri Jan 11 12:49:38 EST 2008


On Thu, 10 Jan 2008, Michael Roth wrote:

> Hello list,
>
> still playing with monads and states, I have the following question:
>
> Given:
>
> 	import Control.Monad.State.Lazy
>
> 	data MyData = MyData { content :: String }
>
> 	foobar :: State MyData String
> 	foobar = do
> 	  gets content
>
> Ok, that looks nice and tidy. But:
>
> 	foobar2 :: State MyData ()
> 	foobar2 = do
> 	  modify $ \x -> x { content = "hello haskell"}
>
> ...looks not so nice.
>
>
> Exists there a way to write this cleaner without writing countless
> "set_xyz" helper functions?

http://www.haskell.org/haskellwiki/Record_access


More information about the Haskell-Cafe mailing list