There's a whole class of languages you may never have heard of, or at least, you don't know what makes them different. Most of us have been using the "practical", popular languages, like C or PHP or Java or Ruby etc. These are all "imperative" languages. IE, they are nothing more than a sequence of one "do this" after another. They are simple to understand, relatively. But there's a class of languages called "Functional languages". You might think, well, all languages have functions, right?
Imperative languages have a lot of limitations you may not be aware of. One obvious limit is that they are a sequence of actions that have to be run more or less in order. That's a problem when you've got a computer with 2 or 4 or more separate "cores", and you can't easily split that program into 2 or 4 or more separate parts to run at the same time. Another is error handling. Do you output an error? Does your function fail? Do you return an error value? And what about those unit tests you are supposed to be doing. How easy are those to do, when you have to set up a test situation for each function or method you want to test, trying to simulate what might actually exist when the program is really running? Or how about debugging, when you have to track down a problem and there are many places where data is changed and used and you don't know which of those are the problem, or if its something else you don't even remember?
A lot of the problems of imperative languages is due to the fact that their functions (and methods in OO programing) are not deterministic. IE, they do not act like the functions in mathematics. They take arguments and return a value, of course, but they can also do any number of things outside their own scope. Like output text for display. Or change a database record. Or flip a pixel on. Or send a signal down a wire that goes somewhere.. That means that you cannot call the function with the same arguments and expect to get the same result. You also cannot assume that the rest of your program or data hasn't changed. That makes testing hard, and debugging hard. And you have to constantly check for errors and decide if you can go on or not to the next function.
Functional languages do away with that because functions are not allowed to do anything other than return something. They are deterministic. You always know that with a certain input you will get the same output. That makes testing really easy. It also makes debugging much easier because you only have to follow the flow, and no need to worry about what might have been changed somewhere else not in that flow. It also makes it much easier to split a program up into different parts that can be run at the same time (or in a different order) because you can easily tell what functions are dependant on others (or not).
Of course, the only problem is, how the hell do you actually do anything if you can't input or output things? For that matter, how do you make sure things happen in the order you want them to (like outputting words in the right order for a grammatically correct sentence).
Monads!
Just to be annoying, stay tuned for the rest of my thought... as soon as I am finished thinking it..
No comments:
Post a Comment