[Haskell-cafe] Advice for clean code.
David McBride
david.mcbride at moxune.com
Mon Dec 3 21:47:48 EST 2007
I am still in the early stages learning haskell, which is my first foray
into functional programming. Well there's no better way to learn than
to write something, so I started writing a game.
Mostly the thing looks good so far, far better than the C version did.
However, my problem is that code like the following is showing up more
often and it is becoming unwieldy.
gameLoop :: World -> IO ()
gameLoop w = do
drawScreen w
action <- processInput
let (result, w') = processAction action w
case result of
MoveOutOfBounds -> putStrLn "Sorry you can't move in that direction."
MoveBadTerrain a -> case a of
Wall -> putStrLn "You walk into a wall."
Tree -> putStrLn "There is a tree in the way."
otherwise -> putStrLn "You can't move there."
otherwise -> return ()
let w'' = w' { window = updateWindowLocation (window w') (location $
player w')}
unless (action == Quit) (gameLoop w'')
Where world contains the entire game's state and so I end up with w's
with multiple apostrophes at the end. But at the same time I can't
really break these functions apart easily. This is error prone and
seems pointless.
I have been reading about control.monad.state and I have seen that I
could run execstate over this and use modify but only if each function
took a world and returned a world. That seems really limiting. I'm not
even sure if this is what I should be looking at.
I am probably just stuck in an imperative mindset, but I have no idea
what to try to get rid of the mess and it is only going to get worse
over time. Any suggestions on what I can do about it?
More information about the Haskell-Cafe
mailing list