Re: Language Purity and Dirty and Clean Functions
Posted: Jul 11, 2006 5:10 AM
Don't worry, you aren't the only one who took FP all wrong. They do it all the time! Even so called developers.
In the first place FP is usually typeless and it's that what makes it so error prone - as in your example.
Pure FP, by disallowing assignments, keeps "states" ordered in time: thus FP works with one or many timelines.
By contrast, in imperative languages it's explicitely how one can handle time: once a state changed, there is no way to tell when (at what point in a programm) did that change occur. So, in case one wants to know that he must deal with time explicitely.
Now let's see wether the order of comutation counts or not.
f(x)=x**2 - 12
An imperative approach:
Compute f(p) and pass the result to g.
Compute that g. (a.k.a. g(f(p))
The two computations yielded on runtime.
A functional approach:
... f(p)+4 ...
... p**2-12+4 ...
... p**2-8 ...
These all took place on compile-time. As a result a new function took shape instead of the old "f" and "g". So, for the run-time, noone can talk about ordering "f" and "g" any longer. It's in this respect order doesn't count!
Of course, at run-time ((p**2)-8) is different than (p**(2-8)) - so in this respect order counts so much!.
The best part of pure FP consists in that the compiler can relate any result solely with te initial input, so it disjoints as many computations as possible. This way these computations could be distributed without the fear of interfearing.
It was that FP are so proud of.