Functional Programming?

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Functional Programming?

Post by nikki »

Have any of you guys messed with 'functional programming'? I got introduced to Haskell by xmonad (a 'monad' is incidentally a Haskell construct), a window manager. I had to use Haskell in the configuration dotfile script xmonad uses to set up my environment, and it was pretty cool, but entirely unlike anything I've seen (mostly imperative languages). Ok, they look cool in theory and all, but are they good in practice? Yes, many projects use them (xmonad is written in Haskell, the xmonad code-base is known for being well-written), but I'm looking for opinions from Ogre-guys, and not just Haskell but also maybe functional programming in general.

(and no, not necessarily for game/3d use, I mean general-purpose)
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Functional Programming?

Post by jacmoe »

I am quite fond of Tcl. :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Functional Programming?

Post by Klaim »

I read a lot of people (particularly on http://stackoverflow.com ) who say that playing with fully functional languages like Haskell, Erlang or OCaml makes you a better (C/C++/C#/Java, etc) programmer. Not sure exactly how, but it seems it allow you to think in a different way than all you're used of in (more or less) object oriented languages.

I also think that Ruby might have the same effect as some way of expression will in this language feel really alien sometimes...
User avatar
RedEyeCoder
Gnome
Posts: 344
Joined: Sat Jun 16, 2007 7:29 am
Location: Brisbane, Australia

Re: Functional Programming?

Post by RedEyeCoder »

jacmoe wrote:I am quite fond of Tcl. :)
Please say it isn't true! :wink: - I have never used tcl, however I have read a few chapters in books about it, it looks like an intense language.

I am the same as Klaim in that I have seen some recent attention on stackoverflow.com towards functional programming.
beaugard
OGRE Contributor
OGRE Contributor
Posts: 265
Joined: Sun Mar 25, 2007 1:48 pm
x 2

Re: Functional Programming?

Post by beaugard »

From what I have read, the increasing interest in functional programming is mainly due to the need for concurrent computing.

Recent article at Dr. Dobbs.
http://www.ddj.com/development-tools/212201710
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Functional Programming?

Post by nikki »

beaugard wrote:From what I have read, the increasing interest in functional programming is mainly due to the need for concurrent computing.

Recent article at Dr. Dobbs.
http://www.ddj.com/development-tools/212201710
Yeah, I read that article. I'm reading the 'Real World Haskell' book online, Haskell is way different from C/C++. Its cool till now (for small things like sort/math-stuff/etc.), but I haven't seen the 'big' things yet so I don't know.
User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
x 18
Contact:

Re: Functional Programming?

Post by PolyVox »

I used a language called SML at university and nearly died. Well, ok, that's an exageration :wink: But I didn't enjoy it as it wasn't particularly practical (had no useful libraries, wasn't used in industry, etc). However, now that I'm older I can see that it is interesting from a more theoretical p[oint of view as you do have to program in a mathematically 'pure' way. I would like to take another look at it sometime.

Incidently, Microsoft has a language called F# coming soon which will provide a functional interface to .NET.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Functional Programming?

Post by xavier »

To the best of my knowledge, only one commercial game has been written using Lisp (as an example) and that wasn't pure Lisp, it was a custom derivative.

Functional programming is still an academic pursuit; the problem is that the systems we write for are still linear/procedural processors -- Von Neumann architecture -- and are not well-suited for functional programming. That's why C/C++ and it's close relatives still rule the roost, so to speak.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Functional Programming?

Post by Kojack »

I've never been able to get the hang of functional languages. I've learnt a ton of languages over the years, but functional languages just never felt right. Of course that's not saying that they are bad. I know most people would be confused by reverse polish notation programming (like in Forth), but I love it.

F# is a .Net version of Ocaml. It looks pretty interesting.

The functional language I want to learn (just for the hell of it, I'm happy using c++/c/lua/ruby/assembly for 99% of my programming work) is Clean.
http://clean.cs.ru.nl
It's got a ton of buzzwords which I'm sure someone out there would get excited about ( http://clean.cs.ru.nl/About_Clean/Clean ... tures.html ).
The main interesting things about it is the IDE which is written entirely in Clean, and it comes with a 2D game library.
http://clean.cs.ru.nl/About_Clean/body_ ... an_ide.htm
http://clean.cs.ru.nl/About_Clean/Platf ... _games.htm
http://clean.cs.ru.nl/platformgameinfo.html

Having a 2d game library available for a language makes it a little more fun to learn than just looking at ascii output.

Game Oriented Assembly Lisp sounds interesting too (used in Jak and Daxter) , but isn't publically released.
lilljohan
Halfling
Posts: 82
Joined: Tue Jan 03, 2006 4:05 pm
Location: Växjö, Sweden
Contact:

Re: Functional Programming?

Post by lilljohan »

I took a course in functional programming about a year ago, were we used Haskell, and it was pretty cool. But I have yet to do something useful with it, although i am taking a course in advanced functional programming right now, so hopefully i will learn enough to actually do something fun =)

If you are interested in functional programming then i recommend that you read this paper http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Functional Programming?

Post by nikki »

Kojack wrote:I know most people would be confused by reverse polish notation programming (like in Forth), but I love it.
One of my friends used Forth, and he said it was a 'beautiful' language. He's so good with reverse polish notation, I gave him a long expression in infix, and he converted it to reverse polish in seconds! He says its some times more natural, you think of "2 3 +" as "2 and 3 added" instead of "2 plus 3". So, "3 4 5 * +" would be "3 and (4 and 5 multiplied) added".
User avatar
_tommo_
Gnoll
Posts: 677
Joined: Tue Sep 19, 2006 6:09 pm
x 5
Contact:

Re: Functional Programming?

Post by _tommo_ »

But why they use reverse notation?
For a coding paradigm that whould be similar to standard math, that's strange.
OverMindGames Blog
IndieVault.it: Il nuovo portale italiano su Game Dev & Indie Games
User avatar
madmarx
OGRE Expert User
OGRE Expert User
Posts: 1671
Joined: Mon Jan 21, 2008 10:26 pm
x 51

Re: Functional Programming?

Post by madmarx »

@xavier
Functional programming is still an academic pursuit; the problem is that the systems we write for are still linear/procedural processors -- Von Neumann architecture -- and are not well-suited for functional programming. That's why C/C++ and it's close relatives still rule the roost, so to speak.
Well I don't think like you at all. Concerning the functionnal, it uses the stack better because of the recurssivity orientation + function pointer, that makes it go as fast or faster than C++. It is also more compiler-time evaluated than c++ (something boost tries to do with MPL & co.).
Check this benchmark :
http://dada.perl.it/shootout/craps.html
There are loads of other benchmarks with same kind of results.

You can read this for more information :
http://www.cs.ubc.ca/~murphyk/Software/ ... ocaml.html
"Furthermore, the quality of code generated by the CAML compiler has been analyzed by the Intel VTune system and it show no pipeline stalls, maximum parallelism between integer and floating point units, and machine assembly code that is as good or better than can be achieved by hand coding."
@ everyone
I did 3 years part time OCaml coding.
I was interviewed (for a job) years ago, and I was asked what my favorite programming language is :
"I think OCaml is the best I can program. It executes as fast or faster than C++, has much more semantic. It is easier to maintain and to read. Unfortunately, most of good available libraries are written in C++, which I like also, even if it is not as good. So, If I have to choose a language, I think I should take a look at what available library exists in which language before taking a decision for a project."

I still think it today. Even if I only use C++ now.
By the way, in boost, there are library that tries to achieve the same level of 'compiler correctness' than functionnal programming with OCaml. (for example a function with 3 arguments fe(a,b,c). You create a new function fe(1,b,c) that you can pass has a parameter, or the MPL).

On the other hand, I know some little prog where done in scheme. I consider it as useful as BF (of course my opinion is a little exagerated).
Tutorials + Ogre searchable API + more for Ogre1.7 : http://sourceforge.net/projects/so3dtools/
Corresponding thread : http://www.ogre3d.org/forums/viewtopic. ... 93&start=0
darke
Halfling
Posts: 94
Joined: Mon Sep 08, 2008 3:30 am

Re: Functional Programming?

Post by darke »

_tommo_ wrote:But why they use reverse notation?
For a coding paradigm that whould be similar to standard math, that's strange.
For more then you'd ever need to know: http://en.wikipedia.org/wiki/Reverse_Polish_notation

IMHO, the nicest feature really is this: "Brackets and parentheses are unnecessary: the user simply performs calculations in the order that is required, letting the automatic stack store intermediate results on the fly for later use. Likewise, there is no requirement for the precedence rules required in infix notation."

Shame I never really had a use for neat languages like prolog/forth/lisp/etc I taught myself as a kid when I had way too much free time. :) They've a lot of little, elegant features that larger languages never really picked up, but they never seemed to catch on. (Well the-operating-system-disguised-as-an-editor, EMACS uses lisp anyway.)
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Functional Programming?

Post by Klaim »

Personally I prefer languages with multi paradigm to benefit from each one where needed. C++ is close to this description but is perfectible.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Functional Programming?

Post by Kojack »

But why they use reverse notation?
For a coding paradigm that whould be similar to standard math, that's strange.
Reverse Polish Notation is incredibly easy to implement.

Polish Notation (or prefix notation) was invented in 1924 by Jan Łukasiewicz. It looks like:
+ 4 8
You give it an operation and 2 arguments.
* + 4 3 8
Would do 4*3+8.

Reverse Polish Notation does things in the opposite way:
4 3 * 8 +
Using a stack, it's very easy to parse and execute an rpn language:
- if the current word is a value (number, string, etc), push the value on the stack.
- if the current word is a binary math symbol (like + * - / % etc) pop the top 2 numbers off the stack and push on the result of the math operation.
- if the current word is a function then execute the functions code. Arguments are popped from the stack and the result of the function is pushed on the stack.
There's no operator precedence, everything happens left to right. You also don't need variables to store values for later use, the stack keeps numbers for you. Although most rpn languages have variables too, to make things easier for the programmer.

HP calculators used to use RPN as their native mode. I still use my 12 year old HP48SX, it rocks.
The fpu inside of x86 cpus also works a bit like rpn.

Ok, sorry for the offtopicness, I'm just an rpn fan. :)
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Functional Programming?

Post by nikki »

The main thing with RPN is that for most expressions, there's no need to 'remember' values anywhere other than the stack. It all hapens in a 'last-in-first-out' kind of order.
User avatar
_tommo_
Gnoll
Posts: 677
Joined: Tue Sep 19, 2006 6:09 pm
x 5
Contact:

Re: Functional Programming?

Post by _tommo_ »

Ok now it is clear :)
Anyway while it's easier to parse for computer, it's somewhat error-prone for us... in an expression with 5-6 terms, it could be difficult to understand what does to what.
Also, it was at first used to avoid hardware limits, but these doesn't exist today...
OverMindGames Blog
IndieVault.it: Il nuovo portale italiano su Game Dev & Indie Games
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Functional Programming?

Post by xavier »

madmarx wrote:@xavier
Functional programming is still an academic pursuit; the problem is that the systems we write for are still linear/procedural processors -- Von Neumann architecture -- and are not well-suited for functional programming. That's why C/C++ and it's close relatives still rule the roost, so to speak.
Well I don't think like you at all. Concerning the functionnal, it uses the stack better because of the recurssivity orientation + function pointer, that makes it go as fast or faster than C++. It is also more compiler-time evaluated than c++ (something boost tries to do with MPL & co.).

The point you are missing, is that we simulate a stack in a linear processor -- it's not a stack-based architecture.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
x 18
Contact:

Re: Functional Programming?

Post by PolyVox »

Klaim wrote:Personally I prefer languages with multi paradigm to benefit from each one where needed. C++ is close to this description but is perfectible.
If you are interested in multi maradigm languages then you might find Falcon intersting. It claims to support procedural, object oriented, prototype oriented, functional, tabular and message oriented programming. I don't even know what all of those are!
User avatar
madmarx
OGRE Expert User
OGRE Expert User
Posts: 1671
Joined: Mon Jan 21, 2008 10:26 pm
x 51

Re: Functional Programming?

Post by madmarx »

The point you are missing, is that we simulate a stack in a linear processor -- it's not a stack-based architecture.
Arrgl, you are right!
Tutorials + Ogre searchable API + more for Ogre1.7 : http://sourceforge.net/projects/so3dtools/
Corresponding thread : http://www.ogre3d.org/forums/viewtopic. ... 93&start=0
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Functional Programming?

Post by xavier »

Sad thing is, something like Lisp is perfect for games programming, since games are all about processing lists (which is, by a happy coincidence, what Lisp stands for).

It's too bad GOAL didn't get more than one game -- I think it may just have been a proof-of-concept anyway and not something that Naughty Dog planned on doing long-term.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
jjp
Silver Sponsor
Silver Sponsor
Posts: 597
Joined: Sun Jan 07, 2007 11:55 pm
Location: Cologne, Germany
Contact:

Re: Functional Programming?

Post by jjp »

Well, even if functional programming doesn't map as good to real computers as procedural programming does: it isn't like all parts of a game's code need to be super-fast. I think the main "problem" is that learning functional programming is not easy, it maps not as good as procedural programming to how humans usually think.
Enough is never enough.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Functional Programming?

Post by nikki »

After a whle reading up on Haskell, I could finally write my own RPN calculator that evaluates its stdin and gives answer to stdout in Haskell:-

Code: Select all

main = interact calc
    where calc str = (show $ head $ foldl oneWord [] (words str)) ++ "\n"
	  oneWord (x:y:xs) ("+") = (x+y) : xs
	  oneWord (x:y:xs) ("-") = (y-x) : xs
	  oneWord (x:y:xs) ("*") = (x*y) : xs
	  oneWord (x:y:xs) ("/") = (y/x) : xs
	  oneWord xs y = (read y) : xs
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Functional Programming?

Post by Kojack »

Knowing absolutely no haskell... is that actually handling rpn or just number number operation triplets? Because rpn can look like: 4 7 2 8 9 + 2 * + + *
which is 4*(7+2+(8+9)*2)
Post Reply