News for hackers. Stuff that matters.
Re:Admitting that Functional Programming Can Be Awkward by jrockway on Tuesday June 16, 2009 @09:31AM (#660119)
I think his program would be harder to implement in C than in Haskell. It's all about what you are experienced with.

(The usual pattern is "transform :: world state 1 -> world state 2". The transformer can "modify" as much of state 1 as it wants.)

Re:Admitting that Functional Programming Can Be Awkward by iamelgringo on Tuesday June 16, 2009 @10:40AM (#660236)
I'm not trying to be snarky, I'm puzzled and interested in learning.

How is passing around world state from one transformer function to another an improvement over traditional stateful programming where functions change variables?

Re:Admitting that Functional Programming Can Be Awkward by stonemetal on Tuesday June 16, 2009 @11:46AM (#660361)
traceability and testability. update world1 is traceable you know just by looking at it what it modifies (sure in the example given it is opaque since it is the entire world) but if you consider that update world is probably broken into update world.player update world.ai etc. Then it becomes easier to trace execution because there is no implicit state. The lack of implicit state also improves testability. I can create a world and send it to update and know that I don't have to do any implicit state priming.
Re:Admitting that Functional Programming Can Be Awkward by richcollins on Tuesday June 16, 2009 @02:10PM (#660591)
A large, complicated world state for a FP language is no simpler than a large complicated graphic of objects w/ inheritance hierarchies. You see, the inheritance hierarchies are the world state. In either system, you have to model the graph in your head to understand how it works. Complexity is the problem, not referential transparency.
Re:Admitting that Functional Programming Can Be Awkward by didroe on Tuesday June 16, 2009 @12:04PM (#660398)
Couldn't you do the same thing by just not using global variables in an imperative language? Then you get the benefits you're mentioning without lots of pointless data copying. You just pass a reference to the world object and you can see exactly what's being modified.
Re:Admitting that Functional Programming Can Be Awkward by stonemetal on Tuesday June 16, 2009 @01:32PM (#660543)
>>without lots of pointless data copying. Think tail recursion. It is easily optimized. It presents a nice uniform interface that forces the right thing. Just look at .net the generated main. It replaces general global scope with an application object global scope that rather encourages "cheating".
Re:Admitting that Functional Programming Can Be Awkward by silentOpen on Tuesday June 16, 2009 @12:34PM (#660451)
It is true that persistence is a default property of functional data structures but with a compiler or interpreter that isn't braindead you shouldn't get "pointless data copying" unless you hold and read both states after the transform. That is, it should be optimized away.
Re:Admitting that Functional Programming Can Be Awkward by didroe on Tuesday June 16, 2009 @01:55PM (#660577)
I stand corrected. Don't you still have to do lots of messing around (in comparison to an imperitive language) to simulate state though?
Re:Admitting that Functional Programming Can Be Awkward by stonemetal on Tuesday June 16, 2009 @02:34PM (#660636)
I am still new to functional programming, but so far it seems like I produce two kinds of code. Functions that gather state and functions that modify state. Say in the above example of update_world world1 all this would do is break apart the world data structure and forward the pieces to their respective update functions and then gather the parts in to a new world. The individual update functions either do the break and gather thing or they do the actual computation on a small entity or set of entities. This makes a large number of small easy to test functions that do the actual work and several "middle layer" functions that are easy to test with stubs.

Though it would be interesting hear how people who have been doing FP for a while handle it.

Re:Admitting that Functional Programming Can Be Awkward by aufreak on Wednesday June 17, 2009 @04:46AM (#661563)
The pure functional world was pretty much impenetrable for me for that very reason ... until I encountered Monads in Haskell. Monads have changed the game completely by providing a very clean encapsulation of side effects in general. There are people who are trying to generalize it even further using what are called "arrows", but just groking Monads in Haskell will give you a new understanding, somewhat along the lines of what learning exterior calculus does to your view of traditional vector calculus.
Re:Admitting that Functional Programming Can Be Awkward by ubernostrum on Tuesday June 16, 2009 @11:35AM (#660341)
His followup addresses this, and brings up some examples which he can't work out how to solve with that pattern.
Re:Admitting that Functional Programming Can Be Awkward by psranga on Tuesday June 16, 2009 @11:55AM (#660378)
Beginner question: does this involve a lot of memory copies which can result in performance problems. "world state" sounds like every data structure in the program.
Re:Admitting that Functional Programming Can Be Awkward by jrockway on Tuesday June 16, 2009 @12:20PM (#660426)
which can result in performance problems

"Premature optimization is the root of all evil."

Computers are fast. On my machine, I can allocate, initialize, and garbage collect almost 4G of memory a second. This sort of thing is really fast, and is unlikely to be a performance problem.

When all of your data is immutable, however, you can reuse the data that you didn't change. The canonical example is a binary tree consisting of 2^n nodes. Changing one node only requires you to allocate n new nodes. As your tree gets bigger, the copying overhead goes to 0.

In the case of Haskell, the compiler is smart enough to figure this all out for you. So all you need to do is write correct code, and let the compiler make it work on a machine.

(It's also theoretically possible to reuse structures by mutating them instead of collecting-and-reallocating. You don't do that as part of your game, though, you let the compiler's optimizer do it. It will do it right, you will do it wrong.)

Re:Admitting that Functional Programming Can Be Awkward by teamonkey on Tuesday June 16, 2009 @07:45PM (#661119)
"On my machine, I can allocate, initialize, and garbage collect almost 4G of memory a second."

When everything has to happen in 1/60th of a second, then all that garbage collection is a serious problem. Memory allocation and deallocation is expensive in games, especially on the consoles.

For simpler games, yes, not a problem.

Re:Admitting that Functional Programming Can Be Awkward by jrockway on Tuesday June 16, 2009 @08:21PM (#661159)
The "you are not Google" principle applies here. Unless you are writing the next hit game for the Wii, you can afford to write maintainable code. If you need every last bit of performance that your (underpowered) hardware can provide, then you need to take shortcuts and hire the 200-member team that that development style entails.
Re:Admitting that Functional Programming Can Be Awkward by teamonkey on Tuesday June 16, 2009 @08:51PM (#661193)
400, in my case.

But the problem is valid. In games every nanosecond counts, which is why the control manual memory allocation offers is so attractive.

It's a problem that even small, single-unit game developers have. On the indie game dev forums, fighting against the .Net/Java GC is a common topic. When the GC kicks in, your framerate usually suffers and you waste productivity with tricks to avoid triggering it. When you have multiple cores, asynchronous GPUs and multi-threaded OSes it affects anything other than the most basic of games.

Having said that, in http://lambda-the-ultimate.org/node/1277 Tim Sweeney (of Epic, developers of Gears of War and the Unreal Engine middleware) says:

"Will gladly sacrifice 10% of our performance for 10% higher productivity" and also "Garbage collection should be the only option"

That presentation is a good summary of the current situation of AAA-games programming (well, three years old, but still) and why Epic are programming in C++ and not Haskell or some other functional language, even though they'd like to. Well worth a read.

Re:Admitting that Functional Programming Can Be Awkward by utx00 on Tuesday June 16, 2009 @12:29PM (#660437)
clojure is smart about it, and only copies what changed. start here: http://clojure.org/state