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

前端 未结 7 1865
醉梦人生
醉梦人生 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:43

    As others have mentioned, the term "pure" in "pure functional programming language" refers to the lack of observable side-effects. For me, this leads straight to the question:

    What is a side-effect?

    I have seen side-effects explained both as

    • something that a function does other than simply compute its result
    • something that can affect the result of a function other than the inputs to the function.

    If the first definition is the correct one, then any function that does I/O (e.g. writing to a file) cannot be said to be a "pure" function. Whereas Haskell programs can call functions which cause I/O to be performed, it would seem that Haskell is not a pure functional programming language (as it is claimed to be) according to this definition.

    For this and other reasons, I think the second definition is the more useful one. According to the second definition, Haskell can still claim to be a completely pure functional programming language because functions that cause I/O to be performed compute results based only on function inputs. How Haskell reconciles these seemingly conflicting requirements is quite interesting, I think, but I'll resist the temptation to stray any further from answering the actual question.

提交回复
热议问题