Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Pure" mean, in the context of programming languages and paradigms?

In other words, what makes a language pure?

For example, Smalltalk is considered to be a purely object-oriented language. Haskell and Lisp are arguably known to be purely functional languages.

When we say pure, does that mean they're not capable of other programming paradigms (which is far from the truth) or does it mean they were designed to be used in that "purely" X paradigm?

like image 507
pat34515 Avatar asked Jul 23 '12 07:07

pat34515


People also ask

What makes a programming language pure?

Pure is a modern-style functional programming language based on term rewriting. It offers equational definitions with pattern matching, full symbolic rewriting capabilities, dynamic typing, eager and lazy evaluation, lexical closures, built-in list and matrix support and an easy-to-use C interface.

What is a pure language?

Benjamin's famous concept of “pure language” invokes an amalgam of all the languages of the world, and it is precisely this aggregate language that is the medium in which the translator should work.

Which is a pure functional programming languages?

Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc. Functional programming languages are categorized into two groups, i.e. − Pure Functional Languages − These types of functional languages support only the functional paradigms.

What is programming in a purely functional style?

Purely functional programming consists of ensuring that functions, inside the functional paradigm, will only depend on their arguments, regardless of any global or local state. A pure functional subroutine only has visibility of changes of state represented by state variables included in its scope.


Video Answer


2 Answers

The word pure has different meanings in different contexts.

Functional Programming

When people talk about Haskell being a pure language, they mean that it has referential transparency. That is, you can replace any expression with its value without changing the meaning of the program. For example, in Haskell:

square :: Int -> Int square x = x * x  main = print (square 4) 

the expression square 4 can be replaced with its value (16) without changing the meaning of the program. On the other, hand, in this Java code:

public int square(int x) {     System.out.println("Done!");     return (x*x); }  public static void main(String [] args) {    System.out.println(square(4)); } 

you can't replace square(4) with its value (16) because it would change the meaning of the program - it would no longer print Done! to stdout. In Haskell it is impossible for functions to have side effects like printing to stdout or changing memory locations, so referential transparency is enforced.

Note that with this meaning of pure, Lisp is not a pure functional language, as its functions can have side effects (if you want to get picky, Haskell isn't a pure functional language because of the existence of unsafePerformIO, but everyone knows that you are consigned to one of the nastier circles of hell if you ever use that function).

Of course, it is always possible to adopt a pure style in an impure language, and many programmers will do this in order to make reasoning about their programs easier. It's just that referential transparency isn't enforced by the compiler, as it is in a pure language.

Examples of purely functional languages include Haskell, Clean and Miranda. Examples of impure functional languages include OCaml, F# and Scheme.

Object Oriented Programming

When people talk about Smalltalk or Ruby being a pure object-oriented language, they mean that there is no distinction between objects and primitive values. In Smalltalk and Ruby, values like integers, booleans and characters are also objects, in the sense that they can receive messages (Smalltalk) or have methods (Ruby). For example, you can do

1.to_s 

in Ruby, i.e. call the method of the integer 1 that converts it to a string. Compare this with an 'impure' OO language like Java, in which there are objects (which are instances of classes, and can have methods, etc) and primitive values (e.g. int, double, bool, which can't have methods).

When an OO language is pure, people often say that "everything is an object", which isn't strictly true (for example, an if statement isn't an object) but is is true to say that "every value is an object".

Examples of pure object oriented languages include Ruby and Smalltalk. Examples of impure object oriented languages include Java and C++.

like image 143
Chris Taylor Avatar answered Sep 22 '22 23:09

Chris Taylor


The word "pure" in functional language is arguably "Stateless", the output does not depend on states.

For imperative languages, you assign x := 0, but you can reassign the value of variable x later. The value of x depends on the current state.

But for pure functional languages, if f(x) = 1, then the result will always be 1.

like image 38
Ronson Avatar answered Sep 22 '22 23:09

Ronson