Stefan O'Rear <[EMAIL PROTECTED]> wrote: > On Tue, Jan 08, 2008 at 09:10:59PM +0100, Achim Schneider wrote: > > That said, I think it's not very Haskell-like to do something > > elegantly in 1000 lines when you can do it in 100 lines and still > > have it look nicer than C. > > I would use IORef State. Making illegal states unrepresentable > greatly helps with code prettiness; the original State allowed > internal aliasing, which is quite definitely silly. > > I think this should be written somewhere as a general rule - when you > have a mutable structure, use a reference to a record, not a record of > references. >
So it would be, instead of ---$<--- idle :: State -> IdleCallback idle state = do (bpx, bpy) <- get $ ballPos state (bvx, bvy) <- get $ ballVel state ballPos state $= (bpx + bvx*td, bpy + bvy*td) ---$<--- type StateRef = IORef State' idle' :: StateRef -> IdleCallback idle' st = do state <- get st let (bpx, bpy) = ballPos' state (bvx, bvy) = ballVel' state st $= state {ballPos' = (bpx+bvx, bpy+bvy)} ---$<--- or, while I'm at it ---$<--- moveBall :: State' -> State' moveBall state = state {ballPos' = (bpx+bvx, bpy+bvy)} where (bpx, bpy) = ballPos' state (bvx, bvy) = ballVel' state idle'' :: StateRef -> IdleCallback idle'' st = st $~ moveBall ---$<--- With the multiple IORef-Model, moveBall looks like this: moveBall :: Vec2 -> Vec2 -> Vec2 moveBall (bpx, bpy) (bvx,bvy) = (bpx+bvx, bpy+bvy) which is IMHO pure Haskell. On the other hand, with the one IORef-Model, the draw function could not possibly mangle any state, which would be a good thing, doing such things can result in keyboards being smashed over heads by team colleagues. I'll think about it and then let you know how to properly tackle the awkward GLpong squad. Generally, in game programming you can have so many state-related bugs that it's infeasible to detect them all statically. You also cheat much, doing stage magic instead of the real stuff, and trading off things all the time. -- (c) this sig last receiving data processing entity. Inspect headers for past copyright information. All rights reserved. Unauthorised copying, hiring, renting, public performance and/or broadcasting of this signature prohibited. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe