Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Everything is An Expression

I've noticed many languages like Ruby and CofeeScript (well a transcompiler) support everything being an expression.

Now it makes the language somewhat simple to understand and definitely seems neat at the surface, but I was looking maybe for some scholarly publications about the positives and negatives of the two approaches.

It would be beneficial if the publications had clear examples that compared the benefits of having everything be an expression vs., well, not.

Examples in CoffeeScript vs Javascript would be nice, but not required.

The concept is definitely cool, but I'm still slightly unsure how revolutionary the whole idea really is (obviously something being revolutionary is somewhat an opinion).

Thanks!

like image 389
Lime Avatar asked Sep 07 '11 00:09

Lime


People also ask

Is every statement an expression?

Any expression could be used as a statement, but not every expression is actually used as a statement.

How is a statement different from expression?

In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.

Which code example is an expression?

In programming, an expression is any legal combination of symbols that represents a value. Each programming language and application has its own rules for what is legal and illegal. For example, in the C language x+5 is an expression, as is the character string “MONKEYS.”

Does scheme use statements and expressions?

Scheme control structures are expressions, and return values. An if expression is a lot like a C if-then statement, but the "then" branch and the "else" branch are also expressions that return values; the if expression returns the value of whichever subexpression it evaluates.


1 Answers

There is nothing revolutionary about this per se. The expression-oriented approach is a functional programming technique.

Expression-oriented code is simpler and less cluttered than statement-oriented code, because of fewer assignments and no explicit return statements. The lack of distinction between expressions and commands enables conceptual uniformity (see Referential transparency) and bottom-up structure.

Some modern languages have adopted functional programming concepts (e.g. C#, Python, Ruby).

Some scholarly insight on the benefits of functional practices:

  • Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs - John Backus

Interesting articles:

  • Why Functional Programming is Important in a Mixed Environment
  • Is C# becoming a functional language?

As to the comment about performance concerns, the possible overhead related to choice of paradigm is probably negligible. Even in C, most statements evaluate as an expression - however, a comparison between a compiled language (C) and an interpreted language (CoffeeScript) is rather useless.

On a theoretical note, an imperative language represents the control flow in more of a machine-oriented way, which may allow for easier hand-optimization than a functional language.

Language performance and its significance depend heavily on the use case. Concerning JavaScript and whatever code transformation on top of it, this performance discussion is completely irrelevant. The gains in productivity outweigh any slight performance hit.

like image 65
makes Avatar answered Oct 01 '22 12:10

makes