What does “pure” mean in “pure functional language”?

前端 未结 7 1850
醉梦人生
醉梦人生 2020-11-30 19:47

Haskell has been called a \"pure functional language.\"

What does \"pure\" mean in this context? What consequences does this have for a programmer?

7条回答
  •  醉梦人生
    2020-11-30 20:34

    A pure function is one which has no side effects — it takes a value in and gives a value back. There's no global state that functions modify. A pure functional language is one which forces functions to be pure. Purity has a number of interesting consequences, such as the fact that evaluation can be lazy — since a function call has no purpose but to return a value, then you don't need to actually execute the function if you aren't going to use its value. Thanks to this, things like recursive functions on infinite lists are common in Haskell.

    Another consequence is that it doesn't matter in what order functions are evaluated — since they can't affect each other, you can do them in any order that's convenient. This means that some of the problems posed by parallel programming simply don't exist, since there isn't a "wrong" or "right" order for functions to execute.

提交回复
热议问题