So for the past year or two I have been reading up on functional programming. I still haven't written anything useful with it but I hope that will change soon. Here is an exchange I had with Chris Seberino on IRC last night which I thought some of you might enjoy.

<tessier> |seb|_: Have you looked at Stackless python? I just suddenly realized it's potential.

<|seb|_> tessier: what is the potential of it? i don't know

<tessier> |seb|_: No stack. Stackless. You can do recursion much more easily without worrying about overflowing the C stack. Also makes threads and coroutines etc much cleaner. EVE Online uses it to script their game engine. BIG MMORPG.

<|seb|_> tessier: yea but how often do you do a massively deep recursion? it may have great use in a few situations...kinda like lisp and haskell

<|seb|_> tessier: also, i try to avoid threads in general and use event driven paradigm

<tessier> |seb|_: Well, there are lots of times you can do massively deep recursion. But you were not taught to use it. You were taught imperative.

<tessier> For example, check out this recursion to add up all of the values of an array in Haskell...

<tessier> sum :: [int] -> Int

<tessier> sum [] = 0

<tessier> sum (x:xs) = x + sum xs

<tessier> You would do that instead of a for loop and an accumulator
variable and a counter variable to index the array in C like languages.

<tessier> This way is less code, fewer variables, mathematically provably correct, less chance of accidentally reusing a loop variable or having scope issues etc.

<tessier> to a functional coder this is just as clear and obvious as a for loop doing the same thing to an imperative coder.

<tessier> And really only that last line is necessary to do the work.

<tessier> The first two lines are boilerplate.

<tessier> Not really boilerplate as they could change but they aren't the guts of it.

<tessier> The first line means this function takes a list of ints and returns an int

<tessier> The second line says if the list is empty the sum is 0

<tessier> The third line implements the actual summing and recursion

<tessier> The (x:xs) part assigns the first int in the list to x and the remainder to xs. This is like car and cdr in Lisp.

<tessier> But each time we call it the first int changes.

--
Tracy R Reed                  Read my blog at http://ultraviolet.org
Key fingerprint = D4A8 4860 535C ABF8 BA97  25A6 F4F2 1829 9615 02AD
Non-GPG signed mail gets read only if I can find it among the spam.

--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to