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:

Re: Functional Programming?

Post by nikki »

Kojack wrote: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)
Yep, it uses a stack (not really a true stack though, its actually a linked list with pushing and popping from the front). Your expression gave me 172 with the program I wrote. I checked with the infix version in a 'usual' calculator, and it's right.

The interesting part here is the (x:y:xs) which 'deconstructs' the list. x and y get assigned to the first and second elements, xs is the list of the rest of the elements. We then assign the stack back to x * y : xs which puts the result of the operation and the rest. One way to construct list is with 'element : list'. This adds element 'element' to the front of the list 'list'. Using this construct as an lvalue 'matches' the pattern and thats how the deconstruction works.

'foldl' is a function that takes a function, an initial value, and a list. It applies the given function to the list and accumulates the result (for the first element, the initial value is taken). In this case, the list we pass is the list of space-seperated words in the expression, and the function is our pattern matching one. 0 is the initial value. Then the patterns are matched (pattern ma'tching is a 'usual' thing, its how parameters to a function are assigned), and the appropriate operation is carried out, and the accumulated value is set to x (op) y : xs (where (op) is the appropriate operator).

Haskell allows you to do some pretty cool things because of its laziness. You can print infinite lists. Because its lazy, they don't get evaluated yet, and it evaluates them as they're being printed. This is an awesome example, it gives an infinite list of fibonacci numbers:-

Code: Select all

fib = 0 : 1 : zipWith (+) fib (tail fib)
The way it does it is really awesome. The 'tail' function just returns the list with its first element removed. 'zipWith' carries out the given operation on the corresoponding elements of the two lists given. Basically, we just add the sequence to a tailed version of itself recursively and get the series:-

Code: Select all

0 : 1 : zipWith (+) (0 : 1 : ...) (1 : ...)
0 : 1 : (0 + 1) : zipWith (+) (1 : 1 : ...) (1 : ...)
0 : 1 : 1 : (1 + 1) : zipWith (+) (1 : 2 : ...) (2 : ...)
0 : 1 : 1 : 2 : (1 + 2) : zipWith (+) (2 : 3 : ...) (3 : ...)
Haskell is lazy, so we don't need the whole list any way. Its done 'on demand'.

I can feel the brain expansion effect kick-in. Haskell really makes you think differently. ;)
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 »

Cool.
I thought the (x:y:xs) was doing a pattern match against the input, getting 2 numbers and a symbol.

If you want something which really makes you think differently, try what I call Constructive Solid Computing.
Basically build a program using physics in a game.
I first played around with it on Quake 1, when a guy showed how you could build the standard logic gates (and, or, not) and more complicated things like flipflops (bistable multivibrators, for those who live in regions where people think a flipflop is a form of footwear) using doors and nail gun turrets. He demoed the technique by using binary logic for a combination lock, but you could do more advanced stuff with it (without touching a line of scripting code. I've also seen a half adder circuit built in Armadillo Run, and of course the calculator built in Little Big Planet.

One of my current goals is to implement a fully working Brainfuck interpreter using game physics. I might try it in halflife 2.
I might have to supply the code to run via a conveyor belt, different positions across the belt are different opcodes.
:)
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 »

Sod Functional Programming - el33t programmers uses esoteric programming languages, like LOLCODE! :D

Code: Select all

HAI
CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
   UP VAR!!1
   IZ VAR BIGGER THAN 10? KTHX
   VISIBLE VAR
IM OUTTA YR LOOP
KTHXBYE  
Pretty cool language! :lol:

I am not kidding: http://en.wikipedia.org/wiki/LOLCODE

<edit>
I wonder how it would feel if Ogre was written in LOLCODE.. :roll: :)
</edit>
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
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 »

LOLCODE is lol. I've seen it before. Not really practically tried it though.
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 »

xavier wrote: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.
Interestingly, I've found it to be quite the reverse, with Lisp:
Lisp maintains the Van Neuman model; there is no sharp, pre-determined border between code and data. Programming in Lisp opens your mind to the power of the Van Neumann model. Programming in Lisp makes you see old concepts in a new light.
http://stackoverflow.com/questions/4724 ... 3696#63696

Mainstream languages, like C++, are slowly moving towards Lisp, but still not quite there yet. :)
It's amazing how advanced that old language is!
I am at day 3 of my Lisp journey, and have already used lambdas and higher order functions.

Lisp is a perfect language for games.
SuperMario 64 was programmed entirely in Lisp -> http://www.franz.com/success/customer_a ... imen.lhtml
Abuse was programmed in C, but used Lisp for logic. -> http://en.wikipedia.org/wiki/Abuse_(computer_game)

But also data driven development, because code and data is unified in Lisp.

I never thought it would have happened, but here I am, programming Lisp in Emacs. :shock:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
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 »

Lisp is an excellent language for expanding your programming consciousness.

Unfortunately, it's also a horrid language for most or the tasks involved with game programming except at the "scripting" layer; for example, trying to work with a graphics API is apples and oranges (with Lisp playing the citrus role). Games *have* been made with it, but it's not as straightforward as it sounds. NaughtyDog spent at least a year making a suitable compiler and dev framework for the game they did in Lisp, so it's not like we can drop everything and start porting Ogre to Lisp. ;)

That's why I still think D is going to be the best practical incremental step away from C++, because it natively supports, with no "bindings" needed, the C-based APIs that we program against in games: the console runtimes, the Win32 API, Linux, etc. It just needs real tool support (i.e. it needs to run in Visual Studio with full debugging support) *and* needs to have dev support on the major consoles, before it "catches on".
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
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 »

jacmoe wrote:Sod Functional Programming - el33t programmers uses esoteric programming languages, like LOLCODE! :D

Code: Select all

HAI
CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
   UP VAR!!1
   IZ VAR BIGGER THAN 10? KTHX
   VISIBLE VAR
IM OUTTA YR LOOP
KTHXBYE  
Pretty cool language! :lol:

I am not kidding: http://en.wikipedia.org/wiki/LOLCODE

<edit>
I wonder how it would feel if Ogre was written in LOLCODE.. :roll: :)
</edit>
I HAS A HAPPI!!!
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
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 »

While it would be *interesting* to do the whole game in Lisp, I was thinking about having the logic in Lisp and the low-level stuff in C++.
Imagine having a self-modifying logic layer. :)
Data driven development? No problem in Lisp.
And the performance is actually quite good, if you compare to other languages.
Using ECL, you can easily use Lisp and C++ together.
About LOLCODE: would be a good exercise to program a LOLCODE interpreter in Lisp. *puts on todo* :wink:

But D is *so* on my list of languages to try.
My only concern is that it would probably mess up my C++.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Brocan
Orc
Posts: 441
Joined: Tue Aug 01, 2006 1:43 am
Location: Spain!!
x 8

Re: Functional Programming?

Post by Brocan »

Functional programming is the hell, and lisp the devil :twisted:
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 »

jacmoe wrote:I never thought it would have happened, but here I am, programming Lisp in Emacs. :shock:
Nooo! Use (g)vim! I've used it with Lisp, there's a Lisp plugin that provides features similar to the Lisp features in Emacs.
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 »

Actually, I stopped using Emacs fairly quickly.
<edit>Not because Emacs is bad. It's just looking really, really ancient.</edit>
Now I'm using ABLE, which is a dead nice Lisp editor, although I miss a debugger.
Does Vim connect to SLIME or similar?
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: Functional Programming?

Post by mkultra333 »

Since esoteric programming languages came up, I just want to make sure people are aware of Malbolge, http://en.wikipedia.org/wiki/Malbolge
Malbolge is a public domain esoteric programming language invented by Ben Olmstead in 1998, named after the eighth circle of hell in Dante's Inferno, the Malebolge.

The peculiarity of Malbolge is that it was designed to be the most difficult and esoteric programming language.

...

Malbolge was so difficult to understand when it arrived that it took two years for the first Malbolge program to appear. The program was not even written by a human being: it was generated by a beam search algorithm designed by Andrew Cooke and implemented in Lisp.

...

This Malbolge program displays "Hello world!", with full capitalization and exclamation mark at the end.

('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#"
`CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~|4XzyTT43Qsqq(Lnmkj"Fhg${z@>
Edit: Oh, just noticed there's a Malbolge SDK. http://blol.org/735-malbolge-desmistificado. It's in Portuguese, but that'll probably be the least of any user's problems. I think I'll implement Malbolge as my project's scripting language.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
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 »

Lolcode has a .net version. I was thinking of trying it with Mogre. :)
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 »

nikki wrote:
jacmoe wrote:I never thought it would have happened, but here I am, programming Lisp in Emacs. :shock:
Nooo! Use (g)vim! I've used it with Lisp, there's a Lisp plugin that provides features similar to the Lisp features in Emacs.
Emacs rocks! :twisted:
emacs.jpg
emacs.jpg (96.39 KiB) Viewed 4323 times
Code completion, intelligent code sense, interactive Lisping - Emacs is perfect for that kind of stuff. :)

(Never thought I'd ever say that!)

And, no: I am not into "Dixie Chicks" - it's one of the practical examples from Practical Common Lisp. :P
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
ceninan
Gnoblar
Posts: 3
Joined: Wed May 20, 2009 6:57 pm

Re: Functional Programming?

Post by ceninan »

jacmoe wrote: I never thought it would have happened, but here I am, programming Lisp in Emacs. :shock:
I know the feeling - I picked up Clojure just recently. Me, programming Lisp off my own accord? And at that, using vim keybindings!? :shock:
Not to mention doing it using Java libraries :roll:.

I never did get used to emacs. Far too much configuration neccessary IMO. And although I've had some bad experiences with vi in the past ("how do I quit/write/do anything!?"), I actually liked it once I did the vimtutor.

That emacs setup is looking much nicer than the default slime buffers, by the way. I think I'll stick to NetBeans and jVi though :wink:.

PS: Haskell scares me. I've always thought that it looks really cool, but whenever I take a closer look, my "heavy math" warning senses begins to tingle.
opengameart.org - free, legal art for open source game projects.
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 »

jacmoe wrote:
nikki wrote:
jacmoe wrote:I never thought it would have happened, but here I am, programming Lisp in Emacs. :shock:
Nooo! Use (g)vim! I've used it with Lisp, there's a Lisp plugin that provides features similar to the Lisp features in Emacs.
Emacs rocks! :twisted:
emacs.jpg
Code completion, intelligent code sense, interactive Lisping - Emacs is perfect for that kind of stuff. :)

(Never thought I'd ever say that!)
Whatever. ;) I've been using (g)vim for quite a while (with the code completion) and stuff, and I absolutely love it. Once you get it configured well, it's really cool, the amount of editing you can do with just a few keystrokes. Emacs might win on the Lisp side, but (g)vim's good for Lisp too, I'd been using it with the 'Practical Common Lisp' book.

Image
jacmoe wrote:And, no: I am not into "Dixie Chicks" - it's one of the practical examples from Practical Common Lisp. :P
Yeah, great book. :-)
warmi
Gnoll
Posts: 674
Joined: Sun May 27, 2007 3:56 am
Location: USA

Re: Functional Programming?

Post by warmi »

xavier wrote:Lisp is an excellent language for expanding your programming consciousness. ,,
Yeah, it is like classical education ... it makes for an excellent topic for all kinds of social (or in this case geeky) gatherings but beyond that hardly anybody has any use for it in the “real word”.
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 »

warmi wrote:Yeah, it is like classical education ... it makes for an excellent topic for all kinds of social (or in this case geeky) gatherings but beyond that hardly anybody has any use for it in the “real word”.
I wonder what real world you're in. :)
Data miners, flight reservation systems, ...
That the language is not mainstream doesn't make it any less useful. Some people realise that.
Lisp is especially well suited to larger, more complex software systems.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
warmi
Gnoll
Posts: 674
Joined: Sun May 27, 2007 3:56 am
Location: USA

Re: Functional Programming?

Post by warmi »

jacmoe wrote:
warmi wrote:Yeah, it is like classical education ... it makes for an excellent topic for all kinds of social (or in this case geeky) gatherings but beyond that hardly anybody has any use for it in the “real word”.
I wonder what real world you're in. :)
Data miners, flight reservation systems, ...
That the language is not mainstream doesn't make it any less useful. Some people realise that.
Lisp is especially well suited to larger, more complex software systems.
Well, when I wrote nobody I didn't literally mean nobody ... think of it as a statistical version of "nobody".

In any case, that's just my personal opinion .. I spent some time programming in Lisp ( college) and found it to be convoluted and non-intuitive - yeah , it does make programming certain tasks much more natural and kind of neat but overall not my cup of tea.
User avatar
aerique
Halfling
Posts: 59
Joined: Thu Oct 18, 2007 2:37 pm
Location: The Netherlands
Contact:

Re: Functional Programming?

Post by aerique »

warmi wrote:In any case, that's just my personal opinion .. I spent some time programming in Lisp ( college)
I've said this in another thread but nothing kills people's enthusiasm for Lisp faster than some professor's convoluted programming problems.

Also Lisp is a family of languages and doesn't specify one specific programming language. For example there's Scheme, a very minimal and pure language generally used at colleges. There's Common Lisp, my favourite, which is very complete and has many warts because of it. There's also Emacs Lisp used in the editor with the same name and many more.
warmi wrote:and found it to be convoluted and non-intuitive - yeah , it does make programming certain tasks much more natural and kind of neat but overall not my cup of tea.
I think people have a specific disposition for programming languages because not everyone's brain works the same way and not everyone approaches problems in the same way. So Lisp might indeed not be your cup of tea, like Algol like languages are not my cup of tea :)

Also Common Lisp isn't strictly functional. It is a multi-paradigm language. For anyone interested: check out Practical Common Lisp, a great book.
Post Reply