29 points · by ingve · 10y ago · 35 comments
To illustrate what I mean by meta layer, the preprocessor (macros) is a (crude) example of meta layer that is (in fact) independent from the C language. In fact, M4 can and has been used on top of many independent languages. But to have a fuller syntax layer, we'll need a more sophisticated (not necessarily more complex) system than M4.
The preprocessor is essentially specified so as to be able to be efficiently implemented inside the tokenizer of a unified C compiler stack (hence why token pasting isn't textual): it isn't really "independent from the C language". In contrast, m4 is fundamentally more "independent" than the C preprocessor, in that (as far as I know... my experience with m4 is much less, and mostly with respect to amateur-level autoconf work) it doesn't assume anything about the underlying text.
I don't think there is anything wrong for the meta layer (syntax layer) to make certain assumptions/restriction for the underlying language layer; and there is nothing wrong for the meta layer to reflect certain characteristics of the underlying languages. The argument is: syntax matters, but it is not necessary to judge (or even reject) a language by its syntax, which may be remedied by a meta layer.
It is not necessary to reject a person by his clothes or even how he talks. The same should go to a programming language.
If we think a programming language as a person, its syntax layer is like the person's look. The person will have a look even without any clothes but the clothes can change his look (syntax matters).
JVM is more like the person's bones (and muscles and other essentials beneath his skin) and Java and Clojure etc. are more like different persons with the same bone structure. Java is a programming language, JVM is not, and the meta-layer I was referring is more like clothes that we can put on the persons.
However, since syntax is the easiest to see and understand, so too it is most often debated. Talking about a language's semantics is hard.
That's not to say that there are not advantages to Lisp syntax. There are and they may outweigh other costs, but I can understand why people object to Lisp aesthetics.
But then I used midje (a Clojure analog to RSpec) with glee, so, by gum, my position is even incoherent.
Go through the code at http://rosettacode.org/wiki/Letter_frequency and whatever your background is you'll see some programs that will look almost incomprehensible and others that you can understand even if you don't know the language they are written into.
And then there is the aesthetic: some of those programs look beatiful, some look ugly. That matters too.
Ada just lets you use characters as indexes. Ada will actually let you use damn near anything as an index. I use enumerated types to build lookup tables for stuff a lot when playing around with it.
array_of_characters = ["a", "b", "a", "c"]
count = {}
array_of_characters.each do |character|
if count.has_key?(character)
count[character] += 1
else
count[character] = 1
end
end
count
=> {"a"=>2, "b"=>1, "c"=>1}
The one on rosettacode is smarter but it takes some time to be understood.Besides the usability issue of introducing the wrong order as noted by the article, it also makes a mess of things like partial application and composition. Consider a language like Haskell where this is a fundamental part how you design function signatures. There is a function for swapping argument order just to make this work. [1]
Had the semantics of the language instead considers binding names more important than ordering there would be no need for that. A -> B -> C would already equal B -> A -> C. In fact you could throw away things like curry and uncurry too if you treated tuples as records, {A,B} -> C would also equal A -> B -> C
Of course now binding names would be much more important than ordering, so something like a rename function would probably still be needed. But it couldn't be much worse than all the workarounds for ordering currently introduced.
[1] http://stackoverflow.com/questions/12129029/how-to-change-th...
E.G: it's very important for the Python community and it one of the selling point of the language.
I tend to think that syntax IS very important for the very same reasons Guido does:
We are basically typers. Making a language with many special charaters hard to type on your keyboard is going to make you work more, no matter how you slice it.
Yet we read code 100x more times than we read it. Hence reading must be efficient.
But there is a soft spot between a code being too long or too concise, either abuse making it harder to scan. And we scan a lot.
Take the colon in Python blocks for example, it is syntaxically useless:
if stuff: # <- the compiler could do without this
foo()
It's main appeal is to improve your hability to scan the code:- your brain can quickly catch all starting block in all nested these indents. - it's way easier to type than "{" - you use it the same way you would do it while writting english (see this very post). It's familiar, and you are already trained by grade school to scan it.
So yes, since you will spend 8h a day reading source code, syntax is of the most importance, so you can express enough, and yet read it without too much though.
Here is how we might do that in Python, a language well known for its clean and simple syntax:
xs = [1, 2, 3, 4, 5, 6, 7]
ys = filter(lambda x: x < 5, xs)
for y in ys:
print y
That seems pretty straightforward to me, though the lambda expression is a little clumsy compared to a language like Haskell that specialises in combining functions neatly: main :: IO ()
main = do
let xs = [1, 2, 3, 4, 5, 6, 7]
let ys = filter (<5) xs
sequence_ $ map (putStrLn . show) ys
The output step in Haskell is a little awkward, as we have to be explicit about sequencing the effects, but this is more of a semantic issue than a syntactic one.Now, let’s look at a C++ version. The C++ standard library has provided built-in filtering algorithms ever since its first standard back in the ’90s, but the syntax if you tried to actually use them was so bad that it took me about 15 minutes to even get this example to work:
#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>
#include <iostream>
#define ARRAY_LENGTH(x) (sizeof(x) / sizeof(*x))
int main()
{
const int xs[] = {1, 2, 3, 4, 5, 6, 7};
std::vector<int> ys;
std::remove_copy_if(xs, xs + ARRAY_LENGTH(xs),
std::back_inserter(ys),
std::not1(std::bind2nd(std::less<int>(), 5)));
std::copy(ys.begin(), ys.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
This was an attempt to show a like-for-like comparison, though it’s a little unfair because it also shows the semantic problem of distinguishing between built-in arrays and the vector container, and it needs a lot of explicit headers where Python and Haskell both happen to make their filter algorithms available as standard. Still, just look at that remove_copy_if mess. Unsurprisingly, approximately no-one ever actually wrote C++ like this for real.We could tidy that up with a few of the more recent improvements, but C++ is still hopelessly clumsy when it comes to employing standard library algorithms:
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
int main()
{
const std::vector<int> xs = {1, 2, 3, 4, 5, 6, 7};
std::vector<int> ys;
std::copy_if(xs.begin(), xs.end(),
std::back_inserter(ys),
[](int x){ return x < 5; });
std::copy(ys.begin(), ys.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
In reality, I suspect many C++ programmers are still just going to write C-style for loops instead for simple tasks like these, and who could blame them? The syntax for the other tools is so bad that programmers will avoid using them, even though the underlying semantics of that copy_if are no different to the clean one-liners in Python and Haskell. mapM_ print ys #include <cstdio>
int main(void) {
const int xs[] = {1, 2, 3, 4, 5, 6, 7};
for (int i : xs) if (i < 5) printf("%d\n", i);
return 0;
}
Maybe I cheated because I use stdio and didn't waste 15 minutes trying to make a useless copy of the array. #include <iostream>
int main()
{
std::cout << "1\n2\n3\n4\n";
}
However, the point of the discussion was the ease or otherwise of performing some basic data manipulation depending on the syntax of the programming language used. Of course we can skip that data manipulation and go direct to the result in this trivial example, but that doesn’t illustrate anything very useful about programming with the different syntactic styles more generally.* shorter
* easier to write
* faster to compile
* way faster to execute in a debug build
Frankly, this doesn't _really_ matter. IIRC, most non-pathological use of the STL compiles into very reasonable code when run through an optimizer.
Let's be real. If you're shipping non-optimized code to a client, you don't actually care about performance. :)