Language Syntax

Started by
50 comments, last by Zahlman 15 years, 6 months ago
This is a pretty general question. What language do you find has the best/easiest to read/cleanest/(insert superlative here) syntax? Ok I'm not trying to start a language war. We all (I imagine) would agree that language syntax isn't the main factor of why someone would use a language; and for the most part one syntax is just as good as the next. I'm just curious as to what ppl have seen that may have made them go, 'oh wow cool', or 'never seen that before'. Or maybe played around with a scripting language or some esoteric language that had a really nice/clean syntax. Or maybe just a general philosophy on why a type of syntax is beneficial for whatever reason.
Advertisement
Ease of reading is a matter of habit. If you spend a few weeks working with a language, you will start finding its syntax clean and nice.

I tend to consider statement-and-expression languages to have an inferior syntax to the expression-only languages. While statements are a reasonable legacy of the good old days, having everything be an expression is a huge boost in expressiveness. Even more so when you have access to anonymous lambdas.
Quote:Original post by ToohrVykI tend to consider statement-and-expression languages to have an inferior syntax to the expression-only languages. While statements are a reasonable legacy of the good old days, having everything be an expression is a huge boost in expressiveness. Even more so when you have access to anonymous lambdas.


Any particular examples you have in mind?
They say that Lisp has a clean syntax, but that doesn't make it easy to read! [smile]

I like python syntax, except that I'm still not entirely convinced about significant whitespace.
I find C# 2.0 syntax to be the best in my experience. Low punctuation ambiguity, little excessive verbosity, only a few 'shapes' in the code and the shapes tend towards similar constructs.

But we'll see. My toy language supports some things that might lead to nicer syntaxes, but it's not there yet. And I can easily admit that improvements can be made than what we've got now.
Quote:Original post by Ryan_001
Any particular examples you have in mind?


Typical Objective Caml code:
let abs x = if x < 0 then -x else x
Equivalent C code, as short as possible:
int abs(int x) { return x < 0 ? -x : x; }


Notice the absence of return statement or brackets in the first code, one of the benefits of expression semantics. Other benefits include throwing an exception or placing an assert at any point of an expression. It also includes placing a switch statement anywhere, instead of having to put it on its own somewhere. Consider a small code sample for choosing a color:

type color = Black | Whitetry   let input = read_line () in  let color = List.assoc [ "black", Black ; "white", White ] input in  printf "You have chosen the %s side\n"    (match color with Black -> "dark" | White -> "light")with Not_found -> (* Thrown by List.assoc *)  printf "Invalid color choice.\n"


Equivalent C++ code:
enum color { Black , White };std::string input;std::getline(std::cin, input);color c;if (input == "black") c = Black;else if (input == "white") c = White;else {   std::cout << "Invalid color choice.\n";   return;}std::cout << "You have chosen the ";switch(input) {  case Black: std::cout << "dark"; break;  case White: std::cout << "white"; break;}std::cout << " side\n";



Quote:Original post by ToohrVyk
type color = Black | Whitetry   let input = read_line () in  let color = List.assoc [ "black", Black ; "white", White ] input in  printf "You have chosen the %s side\n"    (match color with Black -> "dark" | White -> "light")with Not_found -> (* Thrown by List.assoc *)  printf "Invalid color choice.\n"


Whoa, I think my brain just exploded. I see what you mean though, about the switches, and how you can do other stuff like that. I'm curious, what does the in keyword mean? And are the indentation and line returns syntactically significant at all (besides just as token separators)?
Quote:Original post by TelastynBut we'll see. My toy language supports some things that might lead to nicer syntaxes, but it's not there yet. And I can easily admit that improvements can be made than what we've got now.


Sounds interesting. Any examples? Even theoretical ones?
I find python the clean language. I wish other languages would take after it.

class Test():    def __init__(self):        self.foo = "";    def foo(self, x, y):        print x + y


Though I I don't really like weak typed language like Python all the much. I prefer the C/C++ declaration of variables. Python makes it to where you have to indent. Which I love. No more sloppy indention by people because they are too lazy to indent their code.
Quote:Original post by theOcelot
Quote:Original post by ToohrVyk
type color = Black | Whitetry   let input = read_line () in  let color = List.assoc [ "black", Black ; "white", White ] input in  printf "You have chosen the %s side\n"    (match color with Black -> "dark" | White -> "light")with Not_found -> (* Thrown by List.assoc *)  printf "Invalid color choice.\n"


Whoa, I think my brain just exploded. I see what you mean though, about the switches, and how you can do other stuff like that. I'm curious, what does the in keyword mean? And are the indentation and line returns syntactically significant at all (besides just as token separators)?


in does not really have a meaning, it's just a syntactic part of the expression: let <id>=<expr> in <expr>, which introduces a local (readonly) variable. The indentation and newlines have no significance in O'Caml.

F# however, a language based on O'Caml, supports a "light" syntax where some keywords are exchanged for indentation, e.g. the in can be left out but correct indentation is mandatory:
#lightopen System.Collections.Generictype color = Black | Whitetry   let input = Console.ReadLine ()  let color = Map( [ "black", Black; "white", White ] ).[input]  printfn "You have chosen the %s side"    (match color with Black -> "dark" | White -> "light")with   :? KeyNotFoundException -> printf "Invalid color choice.\n"

This topic is closed to new replies.

Advertisement