[Haskell-cafe] Advice for clean code.

Stefan O'Rear stefanor at cox.net
Mon Dec 3 22:17:28 EST 2007


On Mon, Dec 03, 2007 at 08:47:48PM -0600, David McBride wrote:
> 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?

I'd recommend using StateT World IO.  You can always run other functions
using 'lift'; for instance lift can be :: IO () -> StateT World IO ().

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071203/c3be6ffd/attachment.bin


More information about the Haskell-Cafe mailing list