开发者

What do you mean by the expressiveness of a programming language?

I see a lot of the word 'expressiveness' when people want to stress one language is better than the other. But I don't see exactly what they mean by it.

  • Is it the verboseness/succinctness? I mean, if one language can write down something shorter than the other, does that mean expressiveness? Please refer to my other question - Article about code density as a measure of programming language power
  • Is it the power of the language? Paul Graham says that one language is more powerful than the other language in a sense that one language can do that the other language can't do (for example, LISP can do something with macro that the other language can't do).
  • Is it just something that makes life easier? Regular expression can be one of the examples.
  • Is it a different way of solving the same problem: something like SQL to solve the search problem?
  • 开发者_运维技巧

What do you think about the expressiveness of a programming language? Can you show the expressiveness using some code?

What's the relationship with the expressiveness and DSL? Do people come up with DSL to get the expressiveness?


Personally, I feel that the "expressiveness" of a language really comes down to how clearly the language constructs can "express" the developer's intentions.

For example, I feel that C# (especially LINQ via C# 3+) is becoming much more expressive. This LINQ statement is a great example:

var results = collection.Where(item => item > 5);

Without knowing the details of the language or the implementation being used, the developer intent is (in my opinion) very clear in the above statement.

I do not think that the verboseness of the language is equal to its expressiveness, however, there is some correlation in place. If a language requires a lot of code in order to express an abstraction, it is less expressive. These are two related, but different, concepts.

The same is true with power - although here a language's features (ie: power) must be complete enough to express the abstraction clearly. Without this, expressiveness will suffer. That being said, a language can be very "powerful" in terms of features, but not necessarily be expressive, if the feature set is difficult to understand.


"Expressiveness" means the ability to say only what you want done:

bad_event = events.find(&:bad)

rather than how you want it done:

i = 0
bad_event = nil
while i < events.size && bad_event.nil?
  event = events[i]
  if event.bad?
    bad_event = event
  end
  i += 1
end

Among the things that contribute to expressiveness are:

  • A lack of required syntactic sugar
  • First-class functions
  • Garbage collection
  • Either dynamic typing or type inference
  • The language core not being slavishly minimalistic
  • Good functionality in the standard library

To some degree, the expressiveness of any language can be increased by shoving as much "how to do it" off into subroutines/objects as possible so that most of the remaining code is "what to do." The amount of "how to do it" code needed in the most abstract code is one measure of a language's expressiveness: The more the code looks like pseudocode, the more expressive it is of the programmer's intent.

One can also think about the "meta-expressiveness" of a language: How expressive is the language at constructing Domain Specific Languages?


I like Matthias Felleisen's notion of expressive power, which is comparative:

  • Language A is strictly more expressive than language B if both of the following are true:

    • Any program written in language B can be rewritten in language A while keeping the essential structure of the program intact.
    • Some programs written in language A have to be violently restructured in order to be written in language B.

Usually we want to make these comparisons by looking at some kind of "essential core" of a language—for example, maybe we want to consider a dialect of C with only while and not also for and do...while. Or maybe we want to consider a dialect of Perl with only a prefix if form and no unless form. But sometimes these superficial syntactic distinctions are exactly what we mean by "expressive power"; to some programmers it's important to say

die ("found no solutions") unless length(solutions) > 0;

instead of

if (length(solutions) == 0) { die("found no solutions"); }

So you have to establish whether you're asking about expressive power of surface syntax or deeper structure.

The other thing I like about Felleisen's idea is that it admits of the notion of two languages which are definitely different, but neither is more expressive than the other.

You can read a more detailed exposition in the first two pages of his paper On the Expressive Power of Programming Languages. After that comes a lot of pointy-headed theory :-)


If you want an answer that's somewhat theoretical but more rigorous than most, you might want to look around for Matthias Felleisen's On the Expressive Power of Programming Languages. I'm pretty sure a bit of looking around the net will turn up at least a few copies.

If you want a more practical answer of what most people actually mean when they say it, that's, frankly, rather different. At least in my experience, an "expressive" language usually means: "I like the language, but can't cite much (if any) objective support for doing so." Conversely, things like "less expressive", or "not expressive" generally mean: "I don't like the language (or like it less), but can't cite much (if any) objective support for doing so."

"Not expressive" is often similar to a politician accusing another of being "fascist" -- clearly pejorative, but without any meaningful definition of what's supposedly wrong.

One of the big problems stems from a fundamental difference of opinion. There are at least two fundamentally different general ideas that people seem to have about expressiveness:

  1. the ability to express a wide variety of ideas.
  2. the ability to express some specific ideas clearly (and often succinctly).

To consider some extreme examples, assembly language would qualify as highly expressive by the first criteria--you can do essentially anything in assembly language that you can in a higher level language, and you can do some things in assembly language that you can't in essentially any higher level language.

Obviously, assembly language doesn't look nearly so good by the second measure--it typically requires quite a large amount of fairly opaque code to accomplish much. This measure would tend to favor a language like Haskell or APL, to give only a couple of examples.

These two notions of what "expressive" means are frequently close to diametrically opposed. The first tends to favor the "lowest" level languages, while the second tends to favor the "highest" level. At least from what I've seen, most people really start from the language they like, and their definition of "expressive" is basically whatever balance of the two criteria is required for their preferred language to be the "best".


For me, it is the ability of the language to clearly express my logic and ideas through code, in a way that somebody else reading the code can easily figure out what I was thinking when I wrote it.


Wikipedia has a bit about the concept. I myself take it to mean that a language can accomplish more with less (the so called "informal usage" in the Wikipedia article).

I consider JavaScript expressive (though this could be because Douglas Crockford drilled that idea into my noggin) because it can do so much with just a few keywords. For instance, the function keyword is a function, as well as a method, a class, and a lambda.

Some code illustration (leaving out some details for brevity) in JavaScript. It's an event class I wrote:

SJJS.util.Event = (function() {
    var _listeners = [];
    var _listenerReturns = [];

    return {
        addDomListener: function(element, eventName, listener) {
        },
        trigger: function(element, eventName) {
        },
        removeListener: function(eventlistener) {
        }
    }
})();

With just function, var, and some curly braces and parentheses I made a static class with methods and private variables.


Generally speaking, with a programming language which is turing complete you can do anything that another turing complete language can do. That being said, some can do it a lot better than other.

I take expressiveness to mean how much you can say easily, and how well / clearly it can be said. The ability to be terse is part of that ( a very powerful and terse language is one like J ). Generally I find that being concise is a good marker of being expressive. If the language can express a complex operation in a simple manner, it's going in the proper direction.

As to the power, expressiveness isn't all the power of a language. While it may be part of it, speed, security, stability, all of those things factor in as well.

example: summation of a list in Common lisp using the loop operator is concise and expressive

(loop for x in list sum x)


Precision, concision, and readability are the primary components in expressiveness.


I always took it to be roughly equivalent to how high-level a language is. If you wanted to try to quantify expressiveness, the units would be something like "machine code instructions per language statement"

A more expressive language might be very good at doing rather a lot of work without writing a lot of code. However, it would probably be more domain-specific and a wee bit slower for some tasks than a less expressive one.


From Wikipedia: In computer science, the expressive power (also called expressiveness or expressivity) of a language is the breadth of ideas that can be represented and communicated in that language. The more expressive a language is, the greater the variety and quantity of ideas it can be used to represent.

So, I agree. "How easy, comprehensive and composable the language for you to express your intents.": I believe, this is the measure of expressiveness.


QUESTION: Is it the verboseness/succinctness? I mean, if one language can write down something shorter than the other, does that mean expressiveness?

No. For example, is Brainfuck language expressive? I don't think so. Look at an Hello World example in Brainfuck:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Or: hq9plus language. Hello World code:

H

QUESTION: Is it the power of the language? Paul Graham says that one language is more powerful than the other language in a sense that one language can do that the other language can't do (for example, LISP can do something with macro that the other language can't do).

I disagree with Paul. As you see in the above examples, hq9plus language is doing Hello World with one letter: H. Whereas, most of the other languages will do it with much more letters. But, you can create composable and easy to read code with other languages. If hq9plus is doing Hello World with H, does it mean that is it powerful? I believe no.


QUESTION: Is it just something that makes life easier? Regular expression can be one of the examples.

Regexes are great, but sometimes they lose their expressive power. Sometimes, it depends on the programmer.


QUESTION: Is it a different way of solving the same problem: something like SQL to solve the search problem?

Half Yes. SQL is a declarative and very expressive language. Because the underlying engines and technologies can advance and change without you to change your SQL queries. This makes it very expressive. There are many queries have been working for decades and the underlying technology of databases change. But, your queries don't need to. I think this is due to the power of its expressiveness.

I also believe that functional languages are very expressive. Because, you only describe your intent, not your hows, and the underlying technologies can always change and optimize, but, it won't hurt your expressive code.

Example:

// top 10 products with rating higher than 5
return products
  .sort(p => p.rating)
  .filter(p => p.rating > 5)
  .map(p => p.title)
  .take(10)

Above program is expressive, it conveys your intents. And, most probably it won't change when the underlying mechanisms change.


Take for example LINQ. It allows you to use functional programming.

Functional programming emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.

LINQ allows your to express what you want done instead of how to do it. This is a clear example of expressiveness.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜