Difference between revisions of "Pure"

From HaskellWiki
Jump to navigation Jump to search
(Created page with "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....")
 
m
(One intermediate revision by the same user not shown)
Line 2: Line 2:
   
 
* it has no ''side effects'', that is to say, invoking it produces no observable effect other than the result it returns; it cannot also ''e.g.'' write to disk, or print to a screen.
 
* it has no ''side effects'', that is to say, invoking it produces no observable effect other than the result it returns; it cannot also ''e.g.'' write to disk, or print to a screen.
  +
* it does not depend on anything other than its parameters, so when invoked in a different context or at a different time with the same arguments, it will produce the same result.
+
A pure function is trivially [[Referential transparency|referentially transparent]] - it does not depend on anything other than its parameters, so when invoked in a different context or at a different time with the same arguments, it will produce the same result.
   
 
A programming language may be called '''purely functional''' if evaluation of expressions is pure.
 
A programming language may be called '''purely functional''' if evaluation of expressions is pure.
Line 11: Line 12:
 
* [http://conal.net/blog/posts/the-c-language-is-purely-functional The C language is purely functional] (some satire intended),
 
* [http://conal.net/blog/posts/the-c-language-is-purely-functional The C language is purely functional] (some satire intended),
 
* [http://conal.net/blog/posts/is-haskell-a-purely-functional-language Is Haskell a purely functional language?]
 
* [http://conal.net/blog/posts/is-haskell-a-purely-functional-language Is Haskell a purely functional language?]
  +
* [https://stackoverflow.com/questions/4865616/purity-vs-referential-transparency Purity vs Referential transparency], Stack Overflow.
   
 
[[Category:Glossary]]
 
[[Category:Glossary]]

Revision as of 22:59, 5 November 2021

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. In particular,

  • it has no side effects, that is to say, invoking it produces no observable effect other than the result it returns; it cannot also e.g. write to disk, or print to a screen.

A pure function is trivially referentially transparent - it does not depend on anything other than its parameters, so when invoked in a different context or at a different time with the same arguments, it will produce the same result.

A programming language may be called purely functional if evaluation of expressions is pure.

There has been some debate in the past as to the precise meaning of these terms. See also: