Programming language

A comparative approach

''A programming language is a notation for describing a list of instructions to a computer. This article describes the varying sorts of languages and their distinguishing properties.''

A program is a list of instructions that describes how a computer should perform some task. A programming language (or computer language) is a notation for describing a program to a computer. Computer language is also known as code.

Programming languages are studied as a branch of Computer Science, and involve concepts from linguistics, mathematics and logic, engineering, and sometimes philosophy. While similar in structure to human languages at a high level, the syntax and semantics of computer languages diverge significantly from human language due to the need to specify concepts with great precision. Such properties come both from the fact that computers cannot perform inference based on context or previous knowledge the way a person can, and from the need of programming languages to treat with concepts mathematically and unambiguously.

Computer languages, unlike almost all human languages [1], are deliberately constructed. Like human languages, however, there are a colorful variety of languages with differing strengths and weaknesses at expressing certain concepts. Languages change over time as they are adapted to new needs or upgraded to fix discovered weaknesses.

Goals
Programming languages are designed to help people solve problems, especially those of a mathematical or repetitive nature. A language allows the programmer to solve a problem in a generic way, describing its solution in terms of concepts that are relevant to people.

Abstraction
To express the solution in a general way, programmers use abstraction, which refers to the process of describing the unifying concepts or underlying structure of a problem. For example, a common abstract concept within computer science and mathematics is the vector. A vector is an ordered collection of numbers that represents some distance within a physical space along each axis, such as (3, 4), which corresponds to the point (x=3, y=4) as on the Cartesian plane.

The abstraction emphasizes similarities between similar problems: no matter how many physical dimensions there are, one can always represent position with a vector; if two dimensions, then a 2-vector; if three, then a 3-vector, and so on.

Abstraction allows programmers to specify operations or calculations in a general way. For example, one can treat a vector as a distance away from the center of our plane, (0,0). If our vector represents the point (3,4), what is its distance between it and the center? Using the Pythagorean Theorem, we know that distance = square-root(3*3 + 4*4). In this way, a programmer can abstractly define a concept such as the vector's "distance from center" and use the concept in his programs -- when needed, the value will be calculated by the computer. We can generalize further: the Pythagorean Theorem applies to vectors with any number of dimensions, enabling us to write one "vector program" which can find the "distance" of vectors of any sort.

In addition to expressing the solution concisely, abstraction also allows the programmer to solve many different problems with a single piece of code, allowing him to share code and write less overall.

Readability
Computer code can be difficult or easy to understand. Some languages' goal is readability, or the degree to which unfamiliar code is accessible to the reader. Languages which focus primarily on readability mirror English syntax, using familiar words and a human-friendly structure. For example, the following program in BASIC (Beginner's All Purpose Symbolic Instruction Code) asks for a user's name and greets him:

10 INPUT "What is your name: ", U$

20 PRINT "Hello "; U$

Other languages take a different approach, trying to express as much as possible in a short space, such as the languages J and APL, which rely on specialized symbols to represent complex concepts. Fragments of code in J can be cryptic to novices.

(($:@(<#[), (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)

An example of J code.

Imperative vs. Declarative
Languages may be characterized by the approach they offer the user to solving problems. In a declarative language, the programmer describes the properties of input data and the desired properties of the output. The computer uses a problem-solving methodology to find results with the desired properties. When using an imperative language, the programmer provides a specific list of operations that the computer should perform.

As an analogy, a declarative language might allow the programmer to say "Find me a number N where N*N = 49". A human reader recognizes that the problem can be solved with basic algebra; similarly, the computer is "taught" (or rather, provided) with an encyclopedia of strategies it can use to approach such problems. In contrast, in an imperative language the programmer might say: "Compute the square root of 49".

The distinguishing difference between these two categories of language amounts to the fact that in a declarative language the programmer specifies the properties of the output he wants and lets the computer find it, while in an imperative language he describes the list of steps to find it.

The reader might wonder rightly why programmers would choose to work with imperative languages when they look extremely tedious when compared to declarative; describing the answer one wants is usually easier than describing how to find the answer. In practice, declarative languages are limited by the set of problem-solving strategies available to them and by the difficulty of describing some computations as questions. Languages may also offer elements of both approaches.[2]

Example declarative languages:


 * SQL
 * Prolog

Example imperative languages:


 * C++
 * Java

Functional vs. Procedural
Another axis along which languages distinguish themselves is with a functional or procedural approach. Like mathematics, computer programming languages use variables as abstract names for values. In mathematics, although one might express variables as functions of time, a single variable itself does not ever change. Within a particular circumstance, if one says "n = 10", it is a statement of equality.

Functional computer languages mirror mathematics in that their variables are invariant -- once created and as long as they exist, they have a single value. Procedural languages treat variables like a box: the programmer can store a value in the box one moment, then store a different value in it the next.

These two approaches offer a significantly different way of looking at computation. Functional languages model the world as mathematics: computation is the calculation of new values, like the calculation of a function over time. In contrast, procedural languages model computation as change in state: the purpose of computation is to take the computer from one state to another. The state change might be creating a new file on the hard disk or sending some data over the network.

Each approach has advantages and disadvantages. It is usually easier to optimize, or improve the performance, of programs written in procedural languages because the programmer has greater control over the details of a program's execution and a better ability to re-use low-level resources such as memory. Functional languages, since data is not directly modified by the program, make it easier to re-use high-level resources, sharing them between various aspects of the program or operating on them concurrently.

Lazy vs. Eager

(Still to be written....)