3

I am learning functional programming and I can understand why immutability is preferred over mutable objects.

This article also explains it well.

But I am unable to understand why assignments should be performed inside of pure functions.

One reason that I can understand is variable mutability leads to locking and since in a pure function in scala we mostly tail recursion and this creates variables/objects on the call stack rather than a heap.

Is there any other reason why one should avoid variable assignment in functional programming.

Greedy Coder
  • 1,256
  • 1
  • 15
  • 36
  • 1
    A pure function depends only on the parameters given to it. This means that the function returns the same result every time you call it with the same parameters. If the function uses a variable defined outside the function, then it will not return the same result every time. It depends on the variable! – marstran Feb 13 '17 at 17:45
  • take a look at http://stackoverflow.com/questions/1791408/what-is-the-difference-between-a-var-and-val-definition-in-scala – nmat Feb 13 '17 at 17:46
  • What if the variable is local to the function and is not used elsewhere? – Greedy Coder Feb 13 '17 at 17:46
  • @nmat : That talks abt val vs var which I am aware of the differences. But if we are not mutating outside of a functional then does that make the function impure/not functional? – Greedy Coder Feb 13 '17 at 17:47
  • 2
    If the function returns the same result every time (with no side effects) given the same parameters, it is (externally) pure. – marstran Feb 13 '17 at 17:48
  • 2
    I found this question that answers your question: http://softwareengineering.stackexchange.com/questions/196112/in-functional-programming-are-local-mutable-variables-with-no-side-effects-stil – marstran Feb 13 '17 at 17:49
  • @GreedyCoder The function is still functional/pure, but you still might not want to use vars. That thread talks a bit more about that also check the link by marstran. – nmat Feb 13 '17 at 17:50
  • Thanks all. The link from @marstran was all I needed. I am closing this question with that answer link instead of deleting just in case some lost souls like me find this question. – Greedy Coder Feb 13 '17 at 17:52
  • Oh I can't close with answer that's not from SO, that's a dumb rule. Just in case if someone is still searching the link given by marstran answers my question - http://softwareengineering.stackexchange.com/questions/196112/in-functional-programming-are-local-mutable-variables-with-no-side-effects-stil – Greedy Coder Feb 13 '17 at 17:55

1 Answers1

8

There is a difference between assignments and re-assignments. re-assignments are not allowed in functional programming because its mutability is not allowed in pure functions.Variable assignment is allowed.

val a = 1 //assignment allowed

a = 2 //re-assignment not allowed

Reading in a impure fashion (changing state) from the external world is a side-effect in functional programming.

So, function accessing a global variable which can potentially be mutated is not pure.

Just makes life easy.

Generally

When you are disciplined life is less chaotic. Thats exactly what functional programming advocates. When life is less chaotic you can concentrate on better things in life.

So, the main reason for immutability

It becomes hard to reason about the correctness of the program with mutations. In case of concurrent programs this is very painful to debug.

that means it becomes hard to keep track of changes variables undergo in order to understand the code/program or to debug the program.

Mutation is one of the side effect where which makes program hard to understand and reason about.

Functional programming enforces this discipline (use of immutability) so that code is maintainable, expressive and understandable.

Mutation is one of the side effects

Pure function is that one which does not have side effects.

Side effects:

  1. Mutation of variables
  2. Mutation of mutable data structures
  3. Reading or writing to a file/console (external source)
  4. Throwing exceptions to the halt program

Avoiding above mentioned side effects makes a function depend only on the parameters of the function rather than any outside values or state.

Pure function is the most isolated function which neither reads from the world nor writes to the world. It does not halt or break the program control flow.

The above properties make the pure function easy to understand and reason about.

Pure function is mathematical function

Its a mapping from co-domain to range where every value in co-domain is mapped to exactly one value in range.

That means if f(2) is equal to 4 then f(2) is 4 irrespective of what the state of the world is.

Pure function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.

Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40