13

This is a theoretical question, but after many years of programming in what I now realize is "normal" imperative technique, using C++ mainly, I've discovered this other world of functional programming, which I stumbled upon accidentally while casually learning JavaScript.

This has led me to wonder if you could technically replace any complete state-oriented program with a different implementation that is purely functional and without state?

It's an intriguing idea and I must admit that there is a clarity and elegance in functional programming that has really blown my mind.

gnat
  • 20,543
  • 29
  • 115
  • 306
johnbakers
  • 289
  • 1
  • 7

5 Answers5

17

Short answer: yes. According to Wikipedia, the equivalence of lambda calculus to Turing machines as an universal model of computation was shown 1937 by Alan Turing. The computational model of a Turing machine is what you typically have in mind when talking about imperative or stateful programming, and lambda calculus is a mathematically formalization of "pure functional programming".

It is conjected that every effective model of computation is able to carry out the same calculations as a Turing machine, and vice versa. This is called Church-Turing thesis. This conjection, however, cannot be proven, because of the more or less intuitive term "effective model of computation" (perhaps someone will invent a new model in the future?)

Doc Brown
  • 218,378
14

In whatever dynamic system, the "state" is what make your present to be influenced by your past or future (the arrow of time is not a mathematical issue, just a physical constrain).

Whether you have something to "remember" or that depends on what you did, you have a state.

A system with no state is not "dynamic": it is just a combinatorial function. That may not have a state, but -to produce different results- need a state to be somehow supplied.

Now, depending on the computational model you refer to, a state can be represented explicitly (in the form of variable) or implicitly (in the form of "return addresses").

when you do fna(fnb(x)) you are giving a state to fnb that in turn will produce a state for fna. This is due to the fact that x exist before fnb is called (so, it comes from it's own "past").

It's not a matter of "state exisit" or "state don't exist". It's a mater of "I care" or "I don't".

0

State means the ability to respond to a present stimulus in an manner that depends on past stimuli, not just based on the present stimulus.

Purely functional programs are just functions. Thus for practical applications the purely functional program inputs a pair (old_state * present_stimulus) and outputs a pair (new_state * present_response). An external, stateful "looper" is needed to wait for the next stimulus and propagate the state.

A purely functional program doesn't intrinsically have state, and -can't- be used for practical applications directly.

Thus no state-oriented program can be replaced with a different implementation that is purely functional and without state.

Atsby
  • 1,237
0

You can avoid explicit mutable state as long as you don't have to interact with the outside world.

In JavaScript in order for you program to actually have an effect beyond taking up processor cycles, you have to modify the Dom or the Window object, and these API's are stateful. But I suppose you could create a wrapper which passed the Dom and Window objects as parameters to the JavaScript code, and then received a new Dom/Window as output. This would isolate the JavaScript code from mutable state.

Of course you are still relying on state, since the browser window and DOM is stateful by nature. Any interactive application is inherently stateful, but you can still structure your code in such away as to minimize explicit state.

A different question is if it is a good idea. Even Haskell, which is a pure functional language by design, includes the 'state' monad, which allows you to simulate mutable state. This shows that explicit mutable state sometimes really is a desirable pattern.

JacquesB
  • 61,955
  • 21
  • 135
  • 189
0

Think about how you would implement a "state machine" in a programming language without state.

You could probably actually do it but you would end up using function names as storage. Ending up with gobblyday gook like:

if (sm.atBegining()) sm.start() else if (sm.done()) sm.stop() ) else sm.progress()