Large recursive data in Haskell

Programming, for all ages and all languages.
Post Reply
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Large recursive data in Haskell

Post 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.
My OS is Perception.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: Large recursive data in Haskell

Post 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. :)
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
whowhatwhere
Member
Member
Posts: 199
Joined: Sat Jun 28, 2008 6:44 pm

Re: Large recursive data in Haskell

Post 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.
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Re: Large recursive data in Haskell

Post 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.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Large recursive data in Haskell

Post 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:
My OS is Perception.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: Large recursive data in Haskell

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
whowhatwhere
Member
Member
Posts: 199
Joined: Sat Jun 28, 2008 6:44 pm

Re: Large recursive data in Haskell

Post 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.
Post Reply