Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top