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 state or not?

Edit 2 : Thanks for 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.


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

  • 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.


    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.

    State can still be sort of represented using state monads.

    链接地址: http://www.djcxy.com/p/80842.html

    上一篇: 势在必行的混合动力

    下一篇: 功能编程是否避免了状态?