View previous topic - View next topic |
Author |
Message |
valderman Mage
Joined: 29 Aug 2002 Posts: 334 Location: Gothenburg, Sweden
|
Posted: Tue Sep 30, 2008 10:50 am Post subject: Games in functional languages |
[quote] |
|
I'm taking a course in Haskell, a functional language, as part of the CS program at university, and I'm pretty impressed by the power and elegance of the language. To get a bit more intimate with it, I was thinking I'd write a simple game in it. However, since functional programming is completely different from the imperative languages I'm used to, I'm at a loss concerning where to start.
More specifically, it's the lack of concepts such as loops and variables in the imperative sense that has me a bit baffled as to how I could implement a game loop, keep track of state, and so on.
Has anyone here every dabbled in functional languages such as Haskell, Erlang, Lisp, etc. for the purpose of game programming? _________________ http://www.weeaboo.se
|
|
Back to top |
|
|
DeveloperX 202192397
Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
|
Back to top |
|
|
DeveloperX 202192397
Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
|
Back to top |
|
|
RedSlash Mage
Joined: 12 May 2005 Posts: 331
|
Posted: Tue Sep 30, 2008 8:30 pm Post subject: |
[quote] |
|
Haskell... mmm.. fun. Functional programming is quite different, it will prove to be a good challenge. Good luck.
|
|
Back to top |
|
|
valderman Mage
Joined: 29 Aug 2002 Posts: 334 Location: Gothenburg, Sweden
|
Posted: Tue Sep 30, 2008 9:26 pm Post subject: |
[quote] |
|
DeveloperX wrote: | :D I just wrote my first haskell program.
Its nothing big. Its a simple hello world program, but it works.
And is distributed properly.
This is going to prove to be an interesting challenge. (making a game in haskell that is)
Tarball:
http://www.ccpssolutions.com/storage/hs/hello-0.1.tar.gz | After finally getting a hold of my Haskell lecturer (he's been in Canada, of all places, this entire week, and so was unreachable,) I finally got him to give me some pointers. Basically, you can't do loops in Haskell, nor can you keep any global persistent state.
However, you can use recursion to create loops that work just like the ones you'd use in an imperative language, except that it's really recursion and thus looks much cleaner. The trick is that in Haskell tail recursion (when the recursion is the last thing in the function body, thus any state local to the function need not be kept around anymore since it will never be used again) compiles into loops.
As for game state, you simply pass it as an argument to your recursing main loop, passing an updated game state for each recursion. I'm guessing recursive data types will be of tremendous use here. _________________ http://www.weeaboo.se
|
|
Back to top |
|
|
Verious Mage
Joined: 06 Jan 2004 Posts: 409 Location: Online
|
Posted: Tue Sep 30, 2008 11:12 pm Post subject: |
[quote] |
|
Wouldn't recursion in place of a loop create a massively large stack, which would grow larger the longer the program runs?
|
|
Back to top |
|
|
Rainer Deyke Demon Hunter
Joined: 05 Jun 2002 Posts: 672
|
Posted: Tue Sep 30, 2008 11:43 pm Post subject: |
[quote] |
|
SINT NICOLAAS & ZWARTE PIET (Edit: the website seems to be broken, but you can still download the games with source from here.)
Verious wrote: | Wouldn't recursion in place of a loop create a massively large stack, which would grow larger the longer the program runs? |
Not with tail recursion.
|
|
Back to top |
|
|
Verious Mage
Joined: 06 Jan 2004 Posts: 409 Location: Online
|
Posted: Wed Oct 01, 2008 1:32 am Post subject: |
[quote] |
|
Personally, I think a standard for-next loop is more syntactically elegant; the functional language approach seems very roundabout.
|
|
Back to top |
|
|
DeveloperX 202192397
Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
|
Back to top |
|
|
increpare Blue Fillrect
Joined: 19 Feb 2009 Posts: 43
|
Posted: Fri Feb 20, 2009 1:36 am Post subject: |
[quote] |
|
dredging up this old topic, I made a game in haskell (w/ opengl + glut) for a tigsource competition before christmas.
tail recursion isn't necessarily the best way to do loops in haskell. most for loops involve doing something to each element in a list. in this case, using a 'map' function of some sort can be more useful and more straightforward than a for (though you'll likely have to mix it together with some monadic programming quite likely).
For instance, here's some that amounts to something like a
THIS FORUM HATES HASKELL CODE (click on link to view)
it's not particularly efficient, but it worked fine for the prototype I was using it in (to actually get it to combine with the monadic code I had to have the following as well:
> innerMovement :: GameState -> GameState
> innerMovement g = g{entity=moveEntities (entity g) }
> doMovement :: IORef(GameState) -> IO ()
> doMovement gamestate = do
> modifyIORef gamestate innerMovement
-----------
There's also a game-maker like game development tool based around a functional scripting language...ah yes, the clean game library, which I haven't had the chance to try out personally yet.[/url]
Last edited by increpare on Fri Feb 20, 2009 2:52 am; edited 3 times in total
|
|
Back to top |
|
|
DeveloperX 202192397
Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
|
Back to top |
|
|
increpare Blue Fillrect
Joined: 19 Feb 2009 Posts: 43
|
Posted: Fri Feb 20, 2009 2:51 am Post subject: |
[quote] |
|
DeveloperX wrote: | increpare:
You should use the [ code ] bbcode tags when posting code. |
I tried. They don't work. It doesn't like that haskell uses square brackets. It garbles everything. I hadn't noticed that it garbles them just as bad when I leave the tags out, so I just replaced them with a link to the code on pastebin (I left the BBcode tags in the pastebin version; you can test them out yourself if you want).
|
|
Back to top |
|
|
DeveloperX 202192397
Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
|
Back to top |
|
|
Verious Mage
Joined: 06 Jan 2004 Posts: 409 Location: Online
|
Posted: Fri Feb 27, 2009 2:55 am Post subject: |
[quote] |
|
The hosting provider seems to break the forum on a semi-regular basis. :(
|
|
Back to top |
|
|