Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "pure" mean in "pure functional language"?

Haskell has been called a "pure functional language."

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

like image 427
Bobby S Avatar asked Dec 07 '10 22:12

Bobby S


People also ask

What is meant by pure function?

A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. It does not depend on any state or data change during a program's execution. Rather, it only depends on its input arguments.

What does pure mean in Haskell?

From HaskellWiki. A function is called pure if it corresponds to a function in the mathematical sense: it associates each possible input value with an output value, and does nothing else.

What is purity in functional programming?

Functional Purity Purity means that functions have no side effects. The output of a function is predictably the same as long as the input is the same. A pure function isn't affected by and doesn't affect anything outside; it also doesn't mutate any value.

How is a pure functional language distinguished from other functional languages?

A purely functional language is a language which only admits purely functional programming. Purely functional programs can however be written in languages which are not purely functional.


2 Answers

In a pure functional language, you can't do anything that has a side effect.

A side effect would mean that evaluating an expression changes some internal state that would later cause evaluating the same expression to have a different result. In a pure functional language you can evaluate the same expression as often as you want with the same arguments, and it would always return the same value, because there is no state to change.

For example, a pure functional language cannot have an assignment operator or do input/output, although for practical purposes, even pure functional languages often call impure libraries to do I/O.

like image 83
Joel Spolsky Avatar answered Sep 21 '22 17:09

Joel Spolsky


"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.

like image 26
ephemient Avatar answered Sep 22 '22 17:09

ephemient