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

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

    "Pure" and "functional" are two separate concepts, although one is not very useful without the other.

    A pure expression is idempotent: it can be evaluated any number of times, with identical results each time. This means the expression cannot have any observable side-effects. For example, if a function mutated its arguments, set a variable somewhere, or changed behavior based on something other than its input alone, then that function call is not pure.

    A functional programming language is one in which functions are first-class. In other words, you can manipulate functions with exactly the same ease with which you can manipulate all other first-class values. For example, being able to use a "function returning bool" as a "data structure representing a set" would be easy in a functional programming language.

    Programming in functional programming languages is often done in a mostly-pure style, and it is difficult to be strictly pure without higher-order function manipulation enabled by functional programming languages.

    Haskell is a functional programming language, in which (almost) all expressions are pure; thus, Haskell is a purely functional programming language.

提交回复
热议问题