Muhammad Alkarouri
Muhammad Alkarouri

Reputation: 24662

Does functional programming avoid state?

According to wikipedia: functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. (emphasis mine).

Is this really true? My personal understanding is that it makes the state more explicit, in the sense that programming is essentially applying functions (transforms) to a given state to get a transformed state. In particular, constructs like monads let you carry the state explicitly through functions. I also don't think that any programming paradigm can avoid state altogether.

So, is the Wikipedia definition right or wrong? And if it is wrong what is a better way to define functional programming?

Edit: I guess a central point in this question is what is state? Do you understand state to be variables or object attributes (mutable data) or is immutable data also state? To take an example (in F#):

let x = 3
let double n = 2 * n
let y = double x
printfn "%A" y

would you say this snippet contains a state or not?

Edit 2: Thanks to everyone for participating. I now understand the issue to be more of a linguistic discrepancy, with the use of the word state differing from one community to the other, as Brian mentions in his comment. In particular, many in the functional programming community (mainly Haskellers) interpret state to carry some state of dynamism like a signal varying with time. Other uses of state in things like finite state machine, Representational State Transfer, and stateless network protocols may mean different things.

Upvotes: 20

Views: 7962

Answers (5)

Christian Findlay
Christian Findlay

Reputation: 7692

At some point, all apps must have state (some data that changes). Otherwise, you would start the app up, it wouldn't be able to do anything, and the app would finish.

You can have immutable data structures as part of your state, but you will swap those for other instances of other immutable data structures.

The point of immutability is not that data does not change over time. The point is that you can create pure functions that don't have side effects.

Upvotes: 0

aioobe
aioobe

Reputation: 420971

The Wikipedia definition is correct. It may seem puzzling at first, but if you start working with say Haskell, you'll note that you don't have any variables laying around containing values.

The state can still be sort of represented using state monads.

Upvotes: 2

Randolpho
Randolpho

Reputation: 56391

Rather than avoids state, think of it like this:

It avoids changing state. That word "mutable".

Think in terms of a C# or Java object. Normally you would call a method on the object and you might expect it to modify its internal state as a result of that method call.

With functional programming, you still have data, but it's just passed through each function, creating output that corresponds to the input and the operation.

At least in theory. In reality, not everything you do actually works functionally so you frequently end up hiding state to make things work.

Edit:
Of course, hiding state also frequently leads to some spectacular bugs, which is why you should only use functional programming languages for purely functional situations. I've found that the best languages are the ones that are object oriented and functional, like Python or C#, giving you the best of both worlds and the freedom to move between each as necessary.

Upvotes: 12

Chuck
Chuck

Reputation: 237040

I think you're just using the term "state" in an unusual way. If you consider adding 1 and 1 to get 2 as being stateful, then you could say functional programming embraces state. But when most people say "state," they mean storing and changing values, such that calling a function might leave things different than before the function was called, and calling the function a second time with the same input might not have the same result.

Essentially, stateless computations are things like this:

  • The result of 1 + 1
  • The string consisting of 's' prepended to "pool"
  • The area of a given rectangle

Stateful computations are things like this:

  • Increment a counter
  • Remove an element from the array
  • Set the width of a rectangle to be twice what it is now

Upvotes: 25

user405725
user405725

Reputation:

The Wikipedia definition is right. Functional programming avoids state. Functional programming means that you apply a function to a given input and get a result. However, it is guaranteed that your input will not be modified in any way. It doesn't mean that you cannot have a state at all. Monads are perfect example of that.

Upvotes: 3

Related Questions