Does functional programming avoid state?

前端 未结 4 1698
死守一世寂寞
死守一世寂寞 2021-02-01 20:44

According to wikipedia: functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state an

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 20:53

    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

提交回复
热议问题