6 ms·
Why are those brackets there in the syntax? What's the need? It looks hard to read when the programs are bigger. Is there any super advantage to it?
by andrewvijay 10y ago
Why are those brackets there in the syntax? What's the need? It looks hard to read when the programs are bigger. Is there any super advantage to it?
- sn41 10y agoIt is a Lisp thing. If you have an editor like emacs with Scheme support, it is possible to format in a way that makes it quite easy to read: in fact, the formatting reveals the AST. A good explanation is in Chapter 1 of "SICP": https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.1 https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.htm...
- tjpnz 10y agoLisp nostalgia?
- anoctopus 10y agoNostalgia is an odd word to use here when Guile is a Scheme, and so is part of the Lisp family.
- amirouche 10y agois lisp older than C?
- jabl 10y agoYes. It's the second-oldest high level programming language (after Fortran). - Fortran: 1957 - Lisp: 1958 - ... - C: 1972
- amirouche 10y agoI mean to say lisp family. so Fortran counts as part of C family.
- lucozade 10y agoC is part of the Algol family. Fortran was/is syntactically and semantically quite different. Algol was almost contemporaneous with Fortran and, although did learn from Fortran, was, at least partly, a reaction to its perceived flaws.
- skissane 10y agoWell, contemporary Fortran is probably part of Algol family too, since newer versions of Fortran (at least 77 onwards) incorporate the block structured programming concepts from Algol. The "family tree" analogy works imperfectly for programming languages since it is more of a directed cyclic graph than a tree structure – Fortran influenced Algol and then Algol in turn influenced Fortran.
- deleted 10y ago[deleted]
- problems 10y agoIt's a LISP dialect. Brackets are essential to tell the difference between arguments and function calls... take for example in C: foo(bar(1)) would in lisp be (foo (bar 1)). If it were to just be "foo bar 1", you couldn't tell the difference between (C style again for clarity): foo(bar(1)) and foo(bar, 1). Some newer functional languages have a composition operator which allows you to write this without the brackets, but the brackets still are easier to follow in many cases.
- camdez 10y ago> If it were to just be "foo bar 1", you couldn't tell the difference between (C style again for clarity): foo(bar(1)) and foo(bar, 1). Or `foo(bar)(1)`.
- bpicolo 10y agoLisps take a couple months of programming in for it to start being easy to read if you're coming from only c-style languages, but it works out well in the long run. Sure makes refactoring a breeze.
- Fice 10y agoOne big advantage is that this unified recursive representation of code allows structural editing. With things like Emacs's paredit you manipulate code structure directly by splitting, joining, and moving subexpressions instead of editing code as flat lines of characters. Btw, of modern Lisp dialects, I do not like Clojure for undermining this advantage by not syntactically grouping everything that is grouped semantically (e.g. binding pairs in let) just to use less parentheses. In my opinion, Scheme syntax (Guile is a Scheme implementation) is much better.
- didibus 10y agoI think that's a good point, not sure why Clojuse did that. I get confused because some macro expect grouping, others don't. I think it happened as an accident, different people wrote the different core macros and decided on slightly different parsing rules.
- ArneBab 10y agoThe super advantage of Lisp (including Scheme): Its format for defining data is the same as for writing code, making macros a natural part of the syntax: You can change your source code just like you’d change any other list (or rather tree) data type. However all this can be represented fully without parentheses — and this is possible with Guile using a reader extension without losing any of its power. This is realized, for example, in wisp — a language frontend for Guile: http://www.draketo.de/english/wisp http://www.draketo.de/english/wisp It’s a full Scheme, but without the parentheses (behind the scenes it simply infers the parentheses, adds them and hands the result to the regular Scheme frontend in Guile). Wisp is also standardized as Scheme Request For Implementation 119: https://srfi.schemers.org/srfi-119/srfi-119.html https://srfi.schemers.org/srfi-119/srfi-119.html display "Hello World!" ↦ (display "Hello World!") In short: I think this question is justified. But there are many people who start preferring parentheses when they get over the initial readability barrier.
- Slackwise 10y agoAs a simpler solution, making brackets a lighter color so they don't stand out as much. Works really well.
- amirouche 10y agoI use rainbow-delimiter mode in emacs, it of great help.
- branchly2 10y ago> The super advantage of Lisp (including Scheme): Its format for defining data is the same as for writing code, making macros a natural part of the syntax: You can change your source code just like you’d change any other list (or rather tree) data type. I've heard this repeatedly over the years, but the explanation unfortunately always stops right there. Could you please give an example of why you'd want to change your source code programatically? It's always assumed that the reader implicitly knows why this is a good and important thing. Perhaps a practical example showing (A) how this would work, (B) what the benefit is, and (C) how the added work in reasoning is worth that benefit. Note: I personally like the parentheses, as they group everything together so simply.
- marktangotango 10y agoIn lisp/scheme, you write the abstract syntax tree, skipping the parse step entirely.
- mschaef 10y ago> skipping the parse step entirely. Not strictly true... there is still parsing involved in reading Lisp data structures from a character stream. (It's just much less involved than in traditional infix languages.) The way to think of it is this: 1) Lisp has a much more comprehensive (and read/write) syntax for its core data structures. 2) Lisp, the language, is defined in terms of those data structures rather than in terms of character sequences and grammar productions.
- marktangotango 10y agoI think of it as serialing/deserializins the AST to text :)
- mschaef 10y agoI can see that, but it doesn't feel quite right. The tree you get directly from deserialized source text is still a bit more fine grained than I'd expect a true AST to be. To see what I mean, consider this: (if condition 1 2 3) This expression can be deserialized into a Lisp data structure, but it still contains a syntax error, and I don't think a true AST would be able to represent that syntax error.
- zurn 10y agoMany programming languages use braces, parentheses and brackets to convey structure. Javascript and C do it this way: if (foo) { bar(42, zonk()); } Guile/Clojure/etc do it this way: (if foo (bar 42 (zonk))) If you didn't have the separators, programs would be harder to interpret for compilers and human readers, as it's harder to tell what the programmer meant from just if foo bar 42 zonk
- Koshkin 10y agoThis "problem" is not unique to Lisps. Quite often, when I look at a piece of code written in C++, especially when it uses lambda functions inside calls, I can't help asking myself why there are so many brackets (and whether a Lisp would be a better alternative to C++, syntax-wise). The syntax of the lambda itself in C++ is sort of funny: it requires to use all the bracket types at the same time! [](){}
- COMPUTERLOSER 10y agoBut they're not interchangeable, so they give an person reading the code a strong hint whether it's a array index, function call/grouping, or a code block.
- Koshkin 10y agoOh, that's yet another issue: in the context of the above example (which, incidentally, was not merely a list of all kinds of brackets) the meaning of the pair of square brackets is changed from 'array index' to 'lambda'.
- amirouche 10y ago> and whether a Lisp would be a better alternative to C++, syntax-wise IMO it's an open question. There is not much statically typed lisp AFAIK.
- andrewvijay 10y agoWhy are people downvoting for asking a question?