Page 1 of 1

Large recursive data in Haskell

Posted: Fri Aug 28, 2009 5:48 pm
by AndrewAPrice
I'm attempting to use Haskell to build a game as a learning experience. I'm trying to do this a purely functional as possible (with some exceptions such as OpenGL being state based). The thing is, games are nothing but state, so in order to store the state I'll have to pass it back recursively, for example:

Code: Select all

playFrame state = do
	state $= doLogicFrame state
        state $= doRenderFrame state -}
	playFrame state
However, will not all this copying of state around have a severe performance penalty?

It might be okay for a simple Space Invaders game, but the game I'm trying to do has close to 1,000,000 nodes in the terrain plus possibly hundreds of values to pass around. The thought of storing that in a single list that gets copied multiple times per frame makes me shudder.

I could use a difference list, but that could still create somewhat of an unnecessary overhead? And by structuring the list as smaller sublists, it would only have to do a copy of the top level, and not each lower level? For example:

Code: Select all

[Game State,
[] Updatable Objects,
[] All Objects
Root Terrain Node,
Root Collision Quadtree Node,
Root Rendering Node]
And lastly, if something like "All Objects" was a list with 10,000 entries in there, if I reference by ID/index, it will iterate through each node from the start because because Haskell lists are linked lists? The alternative is I use an IO Array, but that would force most of my functions to be monadic, in which case I might as well be using C++.

Thanks.

Re: Large recursive data in Haskell

Posted: Fri Aug 28, 2009 11:31 pm
by Colonel Kernel
You might as well be using C++.

Pure functional languages are a poor fit for something really stateful like the simulation aspect of a game. I suspect this is one reason why Haskell has failed to take the world by storm. :)

Re: Large recursive data in Haskell

Posted: Fri Aug 28, 2009 11:36 pm
by whowhatwhere
Colonel Kernel wrote:You might as well be using C++.

Pure functional languages are a poor fit for something really stateful like the simulation aspect of a game. I suspect this is one reason why Haskell has failed to take the world by storm. :)
Your continuation was discarded before being invoked. You lost the game.

Re: Large recursive data in Haskell

Posted: Sat Aug 29, 2009 12:32 am
by leledumbo
The thing is, games are nothing but state, so in order to store the state I'll have to pass it back recursively
You should use StateMonad.

Re: Large recursive data in Haskell

Posted: Sat Aug 29, 2009 2:12 am
by AndrewAPrice
@leledumbo: Thanks, I'm looking into that now. I'm also making my way through Real World Haskell which so far has been an excellent resource. I don't yet fully understand the state monad (along with a dozen new concepts I'm learning at the same time), so it'll take a lot more research and experimenting. You can ensure more questions will come later! :)

@syntropy and Colonel Kernel: I don't appreciate your comments. I'm doing this as a learning experience for Haskell, so telling me I'm better off using C++ defeats the purpose. :roll:

Re: Large recursive data in Haskell

Posted: Sat Aug 29, 2009 10:09 am
by Colonel Kernel
Sorry, I didn't mean to sound too glib. Learning Haskell is a great idea, I just don't think it's very well suited for creating simulations, so it may be a frustrating experience for you. Use the right tool for the right job...

Maybe try implementing just the physics, AI, etc. for the game in Haskell. They are less state-oriented and the pure functional model may be a better fit.

Re: Large recursive data in Haskell

Posted: Sat Aug 29, 2009 11:25 am
by whowhatwhere
MessiahAndrw wrote:@leledumbo: Thanks, I'm looking into that now. I'm also making my way through Real World Haskell which so far has been an excellent resource. I don't yet fully understand the state monad (along with a dozen new concepts I'm learning at the same time), so it'll take a lot more research and experimenting. You can ensure more questions will come later! :)

@syntropy and Colonel Kernel: I don't appreciate your comments. I'm doing this as a learning experience for Haskell, so telling me I'm better off using C++ defeats the purpose. :roll:
I didn't say use C++. I was saying the Colonel Kernel wasn't contributing anything, and hinting (in an extremely vague way) at how I would solve the problem.