[Haskell-cafe] is there a more concise way to generate helper functions for a datatype built on records?

Thomas Hartman tphyahoo at gmail.com
Sat Nov 24 11:01:01 EST 2007

I think I'm running into more or less the same issue discussed at


Just wondering if I missed anything, or if any of the ideas
considering better records setter/getters have been implemented in the



-- Is there any way to do the following more concisely?
-- Seems like a lot of boilerplate

data GameState = GameState { guesses :: Int, userHighScores ::
UserHighScores, answer :: Maybe Int }
  deriving Show

-- State Helpers ---------------------------------------
modGuesses f gSt  = _set_guesses ( (f . guesses) gSt) gSt
modUserHighScores f gSt = _set_userHighScores ( (f . userHighScores) gSt) gSt
modAnswer f gSt = _set_answer ( (f . answer) gSt) gSt

_set_guesses new gSt   = gSt {guesses=new}
_set_userHighScores new gSt = gSt { userHighScores=new }
_set_answer new gSt = gSt { answer=new }

More information about the Haskell-Cafe mailing list