views:

287

answers:

4

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.

A: 

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.

aioobe
+4  A: 

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.

Randolpho
Can you elaborate on some of those "spectacular bugs"? Also, IMHO Python and C# are rather poor as functional languages.
Mauricio Scheffer
@Mauricio: Python and C# are OO languages with tacked on functional support. They're not pure functional languages. I apologize that I was unclear on that. As for the spectacular bugs... do you know, I can't think of any offhand? They're primarily in UI, IIRC, but... I got nothin'. Sorry.
Randolpho
@Randolpho: _" the best languages are the ones that are object oriented and functional"_ ≪ Scala, F#, and OCaml fit the bill. ;) Agree with @Mauricio that Python and C# are rather poor examples of object-functional languages.
missingfaktor
I'm going to also agree with the three commenter's here. You have a clear inconsistency in your answer; you say python/c# give you the best of both worlds. But, python/c# don't GIVE you the immutability that you discuss in your answer, you have to consciously program in that manner, whereas ocaml, f#, and others innately give it to you.
nlucaroni
@nlucarion: I failed to communicate what I was trying to say properly; for that I apologize. The only reason I haven't attempted to edit it out is because there are comments still referencing my egregious error.
Randolpho
A: 

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.

Vlad Lazarenko
+5  A: 

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
Chuck
Obviously I am conditioned by my background. I used Erlang and I cam across a lot of documentation about _immutable state_. Also, your explanation of stateless computation looks to me similar to what we have in networking protocol: HTTP is a stateless protocol, FTP is not. In these cases we say the state is modelled explicitly, i.e. outside the function. Am I making sense?
Muhammad Alkarouri
Awesome description. +1
Randolpho
Yes, and I think an important aspect to this kind of "state" is its dependence on time. While in imperative languages you're sequencing operations (therefore changing state over time, step by step) really pure functional programs have no inherent notion of time or sequence. They just "are". Of course if you want to execute such a program, the runtime has to somehow map that to changing state over time in some "meaningful" way.
Novox
I don't think string concatenation is inherently stateless. It depends on the string abstraction being immutable. Which it is on .net, but may not be on other languages/platforms.
Mauricio Scheffer