News for hackers. Stuff that matters.

Ask HN: Can someone explain to me why all languages don't try to have the same syntax?

Why does every language need its own syntax? Why does PHP need to use crap like echo and all those dollar signs instead of something more universal?

Why can't all those projects come together and figure out a single universal standard for writing syntax? So that writing code would be exactly the same, no matter what technology you choose to use. At least for the simple stuff.

Why do programmers need to waste time remembering 20 different ways to write something as stupid as a Hello World application?

( 59 comments | 9 points | on HN )
Re:Ask HN: Can someone explain to me why all languages don't... by makecheck on Monday December 22, 2008 @12:14AM (#406111)
In some important cases, languages already have common syntax (such as mathematical operators, regular expressions, and string interpolation such as "\n"). And in those cases, the simplest explanation seems to be: "there was no other sane way to do it".

Some differences really are arbitrary, and could theoretically be merged. Some are historical, e.g. someone worked on Unix for years and thinks in terms of verbs like "echo" because that's what shells used.

But usually, there are very good reasons for any differences.

One reason is the inherent complexity of a parser for a full programming language. It turns out that it's fairly easy to confuse a parser, because they're just not as good as human beings at correctly interpreting the "intent" of a statement. Most languages don't have a reason for being unless they introduce a bunch of unique constructs; and they're lucky if their own constructs can be parsed unambiguously, much less after adding support for extra "standard" constructs from other languages.

Yet another reason is that languages aren't as universal as you may think. For example, what would 'print "hello world"' mean in a makefile...when would the print-out occur? Would languages be allowed to ignore "standard" expressions that they can't logically use, or be forced to invent some interpretation of them?

A final reason is what would happen when languages are embedded in each other, which is even summed up by your example, PHP. If one language is embedded in another, it's a plus that they have different syntax: it clearly separates one from the other. This reduces the risk that you'll have to (for instance) escape or namespace every single name used by the embedded code, to avoid collisions with the surrounding file.

Re:Ask HN: Can someone explain to me why all languages don't... by anamax on Monday December 22, 2008 @02:08AM (#406176)
> In some important cases, languages already have common syntax (such as mathematical operators

Except that they don't. Almost everyone agrees about the relative precedence and associativity of the two argument forms of addition, subtraction, multiplication and division, but that's about it.

And then there are the folks who think that multiplication is denoted by a space. They call themselves mathematicians. Don't they know that space is concatenation or ignored?

> One reason is the inherent complexity of a parser for a full programming language.

For some full programming languages maybe. Other languages are so easy to parse that they added support for roman numerals, turing-complete read macros, and other things.

> It turns out that it's fairly easy to confuse a parser, because they're just not as good as human beings at correctly interpreting the "intent" of a statement.

It turns out that people aren't all that good either - they're subject to a 7+/-2 rule. Plus, very few people can reliably remember 10 levels of operator precedence.

> Most languages don't have a reason for being unless they introduce a bunch of unique constructs; and they're lucky if their own constructs can be parsed unambiguously.

Huh? Apart from some nastiness involving << and >> when C++ templates were introduced, how many programming languages have ambiguous parsings? (Yes, many parse things in ways that some humans find "unintuitive", but that's different.)

Re:Ask HN: Can someone explain to me why all languages don't... by arockwell on Sunday December 21, 2008 @10:07PM (#406013)
A better question is why doesn't everyone in the world speak Esparanto.
Re:Ask HN: Can someone explain to me why all languages don't... by vaksel on Monday December 22, 2008 @01:22AM (#406156)
Because it has to complete with English which is already a world language(at least in the business world)
Re:Ask HN: Can someone explain to me why all languages don't... by jey on Monday December 22, 2008 @12:50PM (#406603)
Re:Ask HN: Can someone explain to me why all languages don't... by tokenadult on Monday December 22, 2008 @12:41AM (#406132)
There is an answer to that question:

http://www.xibalba.demon.co.uk/jbr/ranto/

Esperanto is not particularly easy to learn, despite the claims of its founder, so learners devote their time and effort to learning languages with much more return on investment once the language is learned.

Re:Ask HN: Can someone explain to me why all languages don't... by mooism2 on Monday December 22, 2008 @05:08AM (#406257)
Or even, why isn't every language written using the latin alphabet?

To which an answer might be, latin doesn't support certain sounds, can't represent tones... and if it did, it would break chinese language interoperability.

Re:Ask HN: Can someone explain to me why all languages don't... by jgranby on Monday December 22, 2008 @11:44AM (#406520)
Analogy with human languages isn't ideal, because they have almost all grown up organically, whereas programming languages are, to a large extent, designed. The reason that every language doesn't use the Latin alphabet is, amongst others, geographic -- so many others grew up in isolation from the West, they'd never come across that alphabet before. The same isn't really true of programming languages.
Re:Ask HN: Can someone explain to me why all languages don't... by ardell on Sunday December 21, 2008 @10:11PM (#406017)
The problem with standardization is that you iron out all of the innovations. Certain language constructs are simply better for certain tasks. Lisp is built for recursion so it's great with lists. Object-oriented languages in theory are great for modularity and maintainability. Granted all these aspects have a lot to do with the coder in addition to the language.

If all languages required you to manage memory as in C I'd wager we'd have many fewer entry-level script hackers. Sure, every program would be lickety-split fast, but the App Store certainly wouldn't have 10,000+ apps.

And if every app were written in Java everything would be very portable but also quite slow. It's all about trade-offs and picking the right tool for the right job.

Re:Ask HN: Can someone explain to me why all languages don't... by dhotson on Monday December 22, 2008 @01:11AM (#406145)
I don't mean to be pedantic, but the 'Java is slow' thing simply isn't true anymore. It certainly used to be slow back in the day, but it's actually pretty good these days.

From what I've heard, programmers at Google put it in the same ballpark as C++.

This brings me to another point.. the choice of programming language often has more to do with image and branding as opposed to actual technical merit.

That's why we keep hearing the same old mantras:

Java -> uncool and slow

C/C++ -> fast

PHP -> hack

Ruby -> cool but slow

Cobol -> for dinosaurs

.. even after the problems are fixed, the image remains. It's pretty hard to change people's perceptions.

Re:Ask HN: Can someone explain to me why all languages don't... by jimrandomh on Monday December 22, 2008 @10:01AM (#406405)
JIT compiled languages are slow to load, because they have to compile each bit of code the first time they run it. That is why Java is known as a slow language, even though it does fine in benchmarks: because loading is the one operation that's slow enough for the user to notice.
Re:Ask HN: Can someone explain to me why all languages don't... by silentbicycle on Monday December 22, 2008 @09:27PM (#407177)
Counter-example: LuaJIT (http://luajit.org/luajit_performance.html), largely because the compiler / optimizer themselves are extraordinarily small.
Re:Ask HN: Can someone explain to me why all languages don't... by dhotson on Monday December 22, 2008 @05:27PM (#406960)
The thing is, even compiled C++ apps are no better when loading a bunch of shared libraries etc.

OpenOffice.. I'm looking at you! ;-)

Re:Ask HN: Can someone explain to me why all languages don't... by elai on Monday December 22, 2008 @02:05AM (#406174)
A lot of java software implementations are still very very slow. Try restarting a tomcat servlet container (web server) for some java web project and then try the same with some apache or django dev server. One takes 10 seconds, the other is done in a blink in of an eye. Look at eclipse/netbeans vs. xcode, swing applications vs. wxwidgets applications and so on. Java very bad start up times still makes it effectively slow.
Re:Ask HN: Can someone explain to me why all languages don't... by volida on Monday December 22, 2008 @05:21AM (#406259)
Give me a break. What are you running the tomcat on? an 386? let's go back to Assembly yeii! eat some stones baby
Re:Ask HN: Can someone explain to me why all languages don't... by Hexstream on Sunday December 21, 2008 @11:09PM (#406056)
He asked why we need different syntaxes, not languages. Most of what you said doesn't relate to syntax at all.
Re:Ask HN: Can someone explain to me why all languages don't... by mattmaroon on Monday December 22, 2008 @12:39AM (#406129)
How could you have two different languages with the same syntax? The only thing I can think of is one is a subset of the other.
Re:Ask HN: Can someone explain to me why all languages don't... by DougBTX on Monday December 22, 2008 @03:10AM (#406203)
Since you can write down all abstract syntax trees using the same syntax, and you can convert most languages into an abstract syntax tree (all the popular ones), it would be easy enough to write down most languages using the same syntax.

The real question would be whether that AST syntax would be better than the language's own syntax. I'm guessing it would be harder to read, since a good syntax for an AST would describe only the semantics of the tree, making it harder to pick out the structure of the language itself.

Re:Ask HN: Can someone explain to me why all languages don't... by qqq on Monday December 22, 2008 @01:02AM (#406144)
scheme and common lisp?
Re:Ask HN: Can someone explain to me why all languages don't... by arockwell on Monday December 22, 2008 @01:53AM (#406167)
Isn't scheme a subset of common lisp (at least loosely speaking)?
Re:Ask HN: Can someone explain to me why all languages don't... by Hexstream on Monday December 22, 2008 @12:14PM (#406570)
For one thing, scheme has continuations, a powerful construct that Common Lisp lacks. But it's true that Common Lisp is generally more "featureful".
Re:Ask HN: Can someone explain to me why all languages don't... by arockwell on Monday December 22, 2008 @04:45PM (#406924)
That's a feature that scheme has that common lisp doesn't, but is this supported by a new syntax? I think that it is not clear cut what is a syntactical difference versus a difference in the feature set.

edit: I guess what I'm really asking is there someway in lisp to make a construct that would look just like call/cc? If you could then that's just a feature difference, but if you can't I would agree that's a syntactical difference.

Re:Ask HN: Can someone explain to me why all languages don't... by kragen on Monday December 22, 2008 @03:38AM (#406215)
No, not even close.
Re:Ask HN: Can someone explain to me why all languages don't... by pg on Sunday December 21, 2008 @10:26PM (#406027)
Because syntax is one of the features of a language.
Re:Ask HN: Can someone explain to me why all languages don't... by kwamenum86 on Sunday December 21, 2008 @11:41PM (#406084)
and often times new syntax is created to support features in a language...

...and if you have the same syntax for all languages then what is the difference? All languages would just be extensions of the same basic language if the syntax is the same.

It is a good question though. Why have more than 3 or 4 programming languages at all?

Re:Ask HN: Can someone explain to me why all languages don't... by mattmaroon on Monday December 22, 2008 @02:08AM (#406175)
We have programming languages because people make them. The only way to have only 3 or 4 is to eradicate all but those and stop people from making more. However if we did that, I have a feeling we would now choose 1 or 2 that wouldn't exist if someone had done the same thing a decade ago.
Re:Ask HN: Can someone explain to me why all languages don't... by stcredzero on Monday December 22, 2008 @12:28PM (#406581)
A professor of mine once quipped: "What's the definition of a 'gentleman computer scientist?' Someone who knows how to make a their own computer language, but doesn't."
Re:Ask HN: Can someone explain to me why all languages don't... by mattmaroon on Monday December 22, 2008 @10:36PM (#407225)
What about someone who knows how to get people on eLance to bid to make them for $1,000, no matter how impossible, but doesn't?
Re:Ask HN: Can someone explain to me why all languages don't... by stcredzero on Monday December 29, 2008 @01:12PM (#413235)
If you strip down a language so far that there's not much to it at all, then $1000 for a new language isn't so far fetched. Just don't ask for support for libraries, a JIT VM, etc. Then again, you could implement it on top of a pre-existing VM, and get access to libraries.

What about a minimalist subset of Python with curly braces instead of indenting? I think I'd actually like that language. Add an easy mapping to Python libraries, and you're done. $1000 is still a bit on the low end for that, but $3000 would seem quite reasonable.

Re:Ask HN: Can someone explain to me why all languages don't... by jerf on Sunday December 21, 2008 @10:49PM (#406043)
This question can not be fully answered in prose. It can only be answered with a challenge: Learn four or five diverse languages reasonably well, then try to create the single syntax that works well for all of them. You'll understand when you're done... or perhaps rather, when you find you'll never be done.

I will give one example of the tens upon tens of issues you will encounter: What's the one true syntax for a type declaration? Then, once you've answered that, how does that one true syntax manifest in Python/Perl/Javascript?

Re:Ask HN: Can someone explain to me why all languages don't... by qqq on Sunday December 21, 2008 @11:50PM (#406094)
The one true syntax for type definitions is: don't have special syntax for them. Totally unnecessary. A good general syntax can handle them without a special case.
Re:Ask HN: Can someone explain to me why all languages don't... by agentcoops on Sunday December 21, 2008 @10:26PM (#406028)
Because people have different syntactic preferences? Because some people hate c-style brackets while others hate python-style white-space? While it would make life easier in the short-run for those trying to learn a language who have pre-existing knowledge of the standardized syntax, it's quite possible that some other syntax would lead to wildly improved programmer productivity or, at the very least, much more readable/aesthetically pleasing code.

In general, there seem to be a plethora of values which can be considered in choosing the syntactic form of a language: aesthetics, productivity, tradition, etc. Having different syntax between languages allows different niches for languages within these values not to mention allowing the possibility of great innovations which could improve the experience of programming. Seems unlikely that any current language has the optimal syntax with respect to maximizing all these values and hence it seems foolish to cut off innovation.

Re:Ask HN: Can someone explain to me why all languages don't... by latortuga on Monday December 22, 2008 @04:31AM (#406240)
Python-style colon + whitespace.
Re:Ask HN: Can someone explain to me why all languages don't... by SwellJoe on Monday December 22, 2008 @05:03AM (#406254)
Why does PHP need to use crap like echo and all those dollar signs instead of something more universal?

This strikes me as a bizarre statement. What's more universal than "echo" and "$", which are found in nearly every UNIX shell? UNIX has always been the backbone OS of the Internet, thus a language designed specifically for that purpose would naturally bring along some of those conventions common to administrators experience. (Technically, it probably happened this way in PHP because of the restraints of the way PHP began, but that's not to say $ and echo wouldn't have been natural choices, even if PHP had been more ambitious early on.)

There are many things to complain about in PHP, but these two conventions were not invented for or by the PHP developers.

I get your point, of course...but when you use examples like this, picking on what is very likely the most popular language in the world for developing web applications, it just makes me think you're really saying, "Why does PHP suck?"

Why doesn't Ruby use PHP syntax? After all, if we're going to agree on syntax, shouldn't the one that the largest number of people are already fluent in be the one we choose? Should we go back to earlier languages? Should Python have gone with C-like syntax, like Perl, since C syntax is far more common? Who decides what the right syntax is for all languages to converge on?

I think I have to conclude your question is invalid.

Re:Ask HN: Can someone explain to me why all languages don't... by tlrobinson on Monday December 22, 2008 @01:30AM (#406161)
It's certainly not the only reason, but the same syntax with different semantics could get very confusing.

I think the different syntaxes help you (or at least me) switch contexts and remember what language I'm using.

Re:Ask HN: Can someone explain to me why all languages don't... by cperciva on Sunday December 21, 2008 @11:04PM (#406053)
If every language had its own syntax, how would we make jokes about LISP programs which end with a page of right parentheses?
Re:Ask HN: Can someone explain to me why all languages don't... by jgranby on Monday December 22, 2008 @11:45AM (#406521)
Because the people who designed them didn't agree on what was the best solution for the problem. And the people who use the various languages don't agree, either. Do you really think that you could sit down with a Lisp fanatic and a Java fanatic and get them to decide on a single syntax that they'd each rather use than their own favourite? They'd each prefer to do it their own way.
Re:Ask HN: Can someone explain to me why all languages don't... by nittwerp on Sunday December 21, 2008 @11:35PM (#406077)
Let's say (however unlikely) that all current languages agreed on standard, and the next release of all these languages used this standard. There would inevitably be a group of people who don't agree with the standard and so they go and write a new language that uses their preferred syntax. Now we are back where we started.
Re:Ask HN: Can someone explain to me why all languages don't... by merrick33 on Monday December 22, 2008 @02:18AM (#406183)
hard to market something that is the same as everything else
Re:Ask HN: Can someone explain to me why all languages don't... by bayareaguy on Monday December 22, 2008 @03:35AM (#406211)
I can live with differences in language syntax since in many cases there are semantic reasons behind them. The thing that really gets on my nerves are minor differences in the representations of common data structures (lists, tables, dictionaries, etc).
Re:Ask HN: Can someone explain to me why all languages don't... by asimjalis on Monday December 22, 2008 @12:07AM (#406105)
Yes. I think all languages should be written with s-expressions.
Re:Ask HN: Can someone explain to me why all languages don't... by mattmcknight on Sunday December 21, 2008 @10:16PM (#406020)
If they did have the same syntax, would they be different languages?
Re:Ask HN: Can someone explain to me why all languages don't... by jd on Sunday December 21, 2008 @10:24PM (#406025)
Different semantics
Re:Ask HN: Can someone explain to me why all languages don't... by aaronblohowiak on Sunday December 21, 2008 @10:28PM (#406030)
Same syntax, different semantics.. that does not sound easier to me. At all.

Also, why don't we all just use one operating system for everything, have one kind of coffee, and eat one perfectly balanced meal all the time?

Re:Ask HN: Can someone explain to me why all languages don't... by jd on Sunday December 21, 2008 @10:56PM (#406046)
It seems you're disagreeing with a statement I never made.

The question was "If they did have the same syntax, would they be different languages?"

I replied: "Yes, they would be different because their semantics would be different"

I never said it would be a good thing. And by stretching your analogy to the territory of food it only becomes weaker (with food diversity is a goal by itself). Also it can be argued that using the same operating system for the vast majority of tasks is desirable.

Re:Ask HN: Can someone explain to me why all languages don't... by aaronblohowiak on Sunday December 21, 2008 @11:47PM (#406090)
I don't think I disagreed with you.

I wanted to a) show how "one true syntax" would in itself be challenging and b) explain the utility that diversity has by analogy.

I agree that for most tasks, using a POSIX compliant unix-like system will suffice, but that's as much ground as I'm willing to concede on that point (for now ;)

Re:Ask HN: Can someone explain to me why all languages don't... by helveticaman on Sunday December 21, 2008 @11:17PM (#406061)
I think it is for the same reason that not all cars are shaped the same. Also, to crack wise about lisp.
Re:Ask HN: Can someone explain to me why all languages don't... by shimonamit on Monday December 22, 2008 @07:54AM (#406326)
Because then they would all have the same syntax.
Re:Ask HN: Can someone explain to me why all languages don't... by qqq on Sunday December 21, 2008 @11:48PM (#406091)
It's because some people don't like s-expressions, so they've been making up a bunch of other stuff.
Re:Ask HN: Can someone explain to me why all languages don't... by notyouravgjoel on Sunday December 21, 2008 @11:57PM (#406100)
My theory: people who don't like s-expressions don't get programming;

Thus, everyone who gets programming and is exposed to lisp becomes a lisp programmer,

and people who suck at programming are left out.

Re:Ask HN: Can someone explain to me why all languages don't... by inklesspen on Monday December 22, 2008 @12:07AM (#406106)
I know many people who are counterexamples to your theory. Languages like Perl and Python have a strong advantage in libraries. If you don't need the things that only Lisp can do, there's not as much of a reason to use it.
Re:Ask HN: Can someone explain to me why all languages don't... by pg on Monday December 22, 2008 @11:43PM (#407281)
If you don't need the things that only Lisp can do, there's not as much of a reason to use it.

True. But the Lisp programmer would reply that you need the things only Lisp can do in most non-trivial programs.

Re:Ask HN: Can someone explain to me why all languages don't... by gruseom on Monday December 22, 2008 @09:43PM (#407187)
I think the parent comment was probably trolling, but as someone who loves Lisp I have to say that some people have deeper reasons for not liking it than "libraries". A few of the best programmers I know just don't like Lisp. They tell me s-expressions don't fit the way their brains work. I'm not inclined to dismiss this as laziness, stupidity, or ignorance - though it's tempting :)

If you don't need the things that only Lisp can do, there's not as much of a reason to use it.

This is not an unreasonable thing to say, yet I'm skeptical. The problem is that a lot of the things that are natural to do in language X just wouldn't occur to you in language Y. I think this is a big problem with the way people compare programming languages. I've been meaning to write a post about it sometime.

Re:Ask HN: Can someone explain to me why all languages don't... by coryrc on Monday December 22, 2008 @06:41AM (#406291)
The (derisive) saying used to be "You can write FORTRAN in any language" but you can also write Lisp in any language.

Not that I completely agree with the GP's point.

Re:Ask HN: Can someone explain to me why all languages don't... by gruseom on Monday December 22, 2008 @09:47PM (#407190)
you can also write Lisp in any language

No you can't. In fact, that's precisely what you can't do.

(Unless you're talking about writing a Lisp interpreter... but then your Lisp programs themselves would still be written in Lisp.)

Re:Ask HN: Can someone explain to me why all languages don't... by rsheridan6 on Monday December 22, 2008 @12:20PM (#406573)
I'm a big fan of Lisp, but this is definitely not true. Do you think the CMUCL (a popular Common Lisp application) team who went on to make non-sexp based Dylan are just people who suck at programming? Or the functional programming community in general? Some of the members of that community who are of a certain age would have cut their teeth on Lisp, but they ended up designing/using *ML and Haskell. Or how about Alexander Stepanov, who went from Scheme to C++ and designed the STL? Or Guy Steele, who invented Scheme but went on to help design Java and Fortress? These are not stupid people.
Re:Ask HN: Can someone explain to me why all languages don't... by pg on Monday December 22, 2008 @11:47PM (#407286)
Do you think the CMUCL team who went on to make non-sexp based Dylan are just people who suck at programming?

No, but close. They were designing a language for other people who did. The original intention with Dylan was to create a Lisp that was made more accessible for mainstream programmers.

Ditto Guy Steele on Java: "we were not out to win over the Lisp programmers; we were after the C++ programmers. We managed to drag a lot of them about halfway to Lisp."

Re:Ask HN: Can someone explain to me why all languages don't... by rsheridan6 on Wednesday December 31, 2008 @08:50AM (#415437)
I didn't get to this in a timely manner due to a Christmas vacation, but I'll bite anyway.

I didn't know that Dylan was considered a LFM rather than a LFSP by its creators, but I do know that at least of them gets s-expressions and still thinks they're a bad idea. See Dave Moon's suggestion: http://www.archub.org/arcsug.txt

Re:Ask HN: Can someone explain to me why all languages don't... by nomoresecrets on Monday December 22, 2008 @05:45AM (#406264)
I suspect that this is your hypothesis, rather than your theory.