Expressions vs Statements


A statement is made up of an expression and a terminator. It does not return a value. It’s only way to effect the rest of the program is by side-effects.

Expressions in computing are related to those in mathematics. Statements were a historical necessity. In the beginning computers were very expensive while still only able to do one task at a time. By the time FORTRAN was first in use in 1958, the best way to share the resource was by presenting a program as a stack of punched cards, one per line of code. A standard emerged where each card represented 80 characters. The first 72 were read by the computer. The last 6  could optionally hold a statement number that was ignored by the computer reader. There were large mechanical sorters so that if you dropped these sometimes very large decks of cards then they could be sorted back in order. Programmers were in the habit of numbering cards 10 apart so that new cards could be inserted as the program needed changing. For purely practical reasons each card needed to stand alone so program changes could be effected without re-punching the whole deck.

From another perspective, assembly language representations of computer code are also statements, so perhaps the concept was meant to be.

Every computer language meant for people to read and write need a means of abstraction. With statements we can do that with variables.

var pi = 3.14159;
var radius = 132;
var area = pi * radius * 2;

In Lispz, the expressions can look almost exactly the same.

(ref pi 3.14159)
(ref radius 132)
(ref area (* pi radius 2))

… and I would probably write it that way for clarity, but given that each is an expression, I could also say

(ref area (* (ref pi 3.14159) (ref radius 132) 2))

Under the covers, ref translates to var as Lispz compiles to JavaScript. The are conceptually different – ref is short for reference and implies that it points to the expression rather than being a container for the evaluation result. This is more poignant in languages like Haskell that have lazy evaluation. References in these languages actually point to the expression that is only evaluated the first time it is used. This is like promises in less radical languages.

Another interesting fact. Have you noticed that some languages, like CoffeeScript and Ruby will return the last expression without the need for a return statement? This comes from functional statement-less languages such as Haskell where the only value a function can return is the last (and in the case of Haskell only) expression.

A clean functional programming function (say that three times quickly) will have references for clarity and one expression. Haskell enforces this, while others such as Lispz leave this to the programmer.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s