How can you do anything useful without mutable state?

后端 未结 18 1651
栀梦
栀梦 2020-11-28 17:12

I\'ve been reading a lot of stuff about functional programming lately, and I can understand most of it, but the one thing I just can\'t wrap my head around is stateless codi

相关标签:
18条回答
  • 2020-11-28 17:46

    By using lots of recursion.

    Tic Tac Toe in F# (A functional language.)

    0 讨论(0)
  • 2020-11-28 17:48

    It's just different ways of doing the same thing.

    Consider a simple example such as adding the numbers 3, 5, and 10. Imagine thinking about doing that by first changing the value of 3 by adding 5 to it, then adding 10 to that "3", then outputting the current value of "3" (18). This seems patently ridiculous, but it is in essence the way that state-based imperative programming is often done. Indeed, you can have many different "3"s that have the value 3, yet are different. All of this seems odd, because we have been so ingrained with the, quite enormously sensible, idea that the numbers are immutable.

    Now think about adding 3, 5, and 10 when you take the values to be immutable. You add 3 and 5 to produce another value, 8, then you add 10 to that value to produce yet another value, 18.

    These are equivalent ways to do the same thing. All of the necessary information exists in both methods, but in different forms. In one the information exists as state and in the rules for changing state. In the other the information exists in immutable data and functional definitions.

    0 讨论(0)
  • 2020-11-28 17:48

    You can't have a pure functional language that is useful. There will always be a level of mutability that you have to deal with, IO is one example.

    Think of functional languages as just another tool that you use. Its good for certain things, but not others. The game example you gave might not be the best way to use a functional language, at least the screen will have a mutable state that you can't do anything about with FP. The way you think of problem and the type of problems you solve with FP will be different from ones you are used to with imperative programming.

    0 讨论(0)
  • 2020-11-28 17:50

    Bear in mind: functional languages are Turing complete. Therefore, any useful task you would perform in an imperitive language can be done in a functional language. At the end of the day though, I think there's something to be said of a hybrid approach. Languages like F# and Clojure (and I'm sure others) encourage stateless design, but allow for mutability when necessary.

    0 讨论(0)
  • 2020-11-28 17:51

    In addition to the great answers others are giving, think about the classes Integer and String in Java. Instances of these classes are immutable, but that doesn't make the classes useless just because their instances cannot be changed. The immutability gives you some safety. You know if you use a String or Integer instance as the key to a Map, the key cannot be changed. Compare this to the Date class in Java:

    Date date = new Date();
    mymap.put(date, date.toString());
    // Some time later:
    date.setTime(new Date().getTime());
    

    You have silently changed a key in your map! Working with immutable objects, such as in Functional Programming, is a lot cleaner. It's easier to reason about what side effects occur -- none! This means it's easier for the programmer, and also easier for the optimizer.

    0 讨论(0)
  • 2020-11-28 17:52

    Here's how you write code without mutable state: instead of putting changing state into mutable variables, you put it into the parameters of functions. And instead of writing loops, you write recursive functions. So for example this imperative code:

    f_imperative(y) {
      local x;
      x := e;
      while p(x, y) do
        x := g(x, y)
      return h(x, y)
    }
    

    becomes this functional code (Scheme-like syntax):

    (define (f-functional y) 
      (letrec (
         (f-helper (lambda (x y)
                      (if (p x y) 
                         (f-helper (g x y) y)
                         (h x y)))))
         (f-helper e y)))
    

    or this Haskellish code

    f_fun y = h x_final y
       where x_initial = e
             x_final   = loop x_initial
             loop x = if p x y then loop (g x y) else x
    

    As to why functional programmers like to do this (which you did not ask), the more pieces of your program are stateless, the more ways there are to put pieces together without having anything break. The power of the stateless paradigm lies not in statelessness (or purity) per se, but the ability it gives you to write powerful, reusable functions and combine them.

    You can find a good tutorial with lots of examples in John Hughes's paper Why Functional Programming Matters.

    0 讨论(0)
提交回复
热议问题