Reputation: 3374
I hope I am contemplating my question correctly.
As far as my understanding, imperative programming (to the very brief) is based on mutation, control structures such as loops and assignments.
While i was following a course about functional programming they have mentioned a point. I want to understand it to depth..
The point is:
If we want to implement high-level concepts following their mathematical theories, there's no place for mutation.
What i have understand from this is, value/co-efficient assigned to particular variable should be same and could not be reassigned time to time to create abstract definition out of it. is it correct ?
Can you add some more points ?
Upvotes: 0
Views: 486
Reputation: 8197
You can think of it this way. At the beginning of the 20th century the level of mathematics has become so high that scientists have been able to apply it to the study of mathematical methods, specifically calculations and evidences.
As a result of research there are two main approaches: Turing machine and lambda calculus.
The first approach (Turing machine) have an idea about a state that is recorded on a tape, and that can be changed. Also this approach have an idea about an algorithm as a sequence of steps.
In the second approach (lambda calculus) any calculation is regarded as a combination of a several functions.
F.e. let's see Taylor series of a sine function:
First expression gives us an example of functional calculation. There's no steps to calculate sine, there's just combination of functions. An input of these functions is two things: the x
and the series of natural numbers 1, 2, ...
. We're mapping some complex function to each element of the series, then we're reducing new series to a single value with a help of function +
.
Second expressions geves us an example of imperative calculation. At least we can see here separate steps of calculating.
An exciting introduction to the functional programming you can find in the book Structure and Interpretation of Computer Programs.
Upvotes: 2
Reputation: 931
It's quite correct - if you don't change your variable values it's much easier to prove that your program is correct.
If you have mutable state, your program depends on your environment, and function call order may be important. In situation like
var a = "test"
def foo() = {
a = "foo"
}
def bar() = a
both of your functions may work correctly, but their composition may fail (if you expect "test" from bar()). In functional approach if parts of your program are correct, then your program as a whole is correct.
Upvotes: 0
Reputation: 1741
If there is no mutation, there are no variables necessary. That's first.
More important is that imperative programming is good for implementing algorithms - step by step instructions how to do something, while functional programming is better to implement concepts - terms defined with other terms etc. Functional programming is more close to mathematical theories, which usually base on definitions of something and not algorithms.
Upvotes: 0