6 ms·
Indenting styles is, humorously, a massive problem for the lisps. Consider, say, for (i in [1, 2, 3]) { x(i); y(i); z(i); } The indentation h
by roenxi 6y ago
Indenting styles is, humorously, a massive problem for the lisps.
Consider, say,
for (i in [1, 2, 3]) {
x(i);
y(i);
z(i);
}
The indentation here very naturally demarcates between control structures and non-control code.
Compared to similar lisp-y code:
(map
#(-> % x y z)
(range 3))
The indentation doesn't give the programmer any hints that map is actually a control structure fulfilling the same role as a for loop.
Given that indentation is critical enough to be a major language feature of something like Python, lisps have a weakness here that isn't dealt with very often. This so-called 'semantic indenting' doesn't go far enough - programmers want much more of it if Python and C-like languages are any indicator. I think there is a very desirable equilibrium here; something where the indentation changes based on the semantic intention of the function. I don't want all my Clojure functions to indent the same way, map/reduce/recur/let/etc are special.
- rscho 6y agoLisp programmers have SLIME or equivalent, rainbow-parens and the like. They trade syntactic cues for semantic editing capabilities, which is why indentation/syntactic cues are less of a big deal in lisp communities and at the same time one the reasons that keep lisp in the shadows of the programming world.
- pogorniy 6y agoWhen you write list, you can't know which of the structures are control and which are not (and why would you in the first place?). Because macroses can give any semantics, including control one, to the provided list. You think of the structures as lists, and care about narrative "all children of the list are at the same indentation level". That's where I find this "semantic formatting" a useful thing.
- roenxi 6y agoI think your point might go to issues implementing a solution technically? Just in case it does... It's only alluded to in this article, but Clojure has a semi-community-standardised metadata format [0] for indicating how a function should be indented. It isn't very impressive, and I'm not saying this is a solved problem because it isn't. I've never seen well indented lisp code in the way that Python is excellent. But the technical aspects of how to solve it are implemented, the answer is 'add semantic metadata to the functions, have a Well Known Standard for indenting code semantically'. There is no reason in principle that Clojure couldn't have Python-good indenting except nobody seems to have any good ideas for how to format code usefully. Probably a style breakthrough waiting to happen. [0] https://docs.cider.mx/cider/indent_spec.html https://docs.cider.mx/cider/indent_spec.html
- bluefox 6y agoMassive problem? That's news to me. But what do I know, I've only been writing Lisp for more than a decade. On the other hand, the Clojure kids seem to reinvent programming badly while maligning other Lisps (see blog post), so maybe they do have a massive problem with trivial things like indentation.
- dan-robertson 6y agoI think the Clojure people tend to be pretty thoughtful about language design and doing things in ways that will be functional and composable. Common Lisp is a language with a lot of warts where many features were tacked on but not properly integrated with everything else. It also suffers from the concerns and technology of the 1980s (a big focus on mutability, no multithreading in the spec, the pathnames library, no immutable maps, to name a few.) Scheme is minimal and that can be an advantage but Clojure has different goals to scheme
- bluefox 6y agoI think the Clojure people tend to produce unreadable mess, which tells me the design of their language is down in the gutter. Common Lisp is a language with a lot of warts, sure, but typical Common Lisp code is way, way, way more tasteful than typical Clojure code. We like our multiparadigmness, thank you very much. That includes being able to mutate things. We also like that the language is stable, and that means no changes to the spec, and that means it won't cover everything. That's OK, because our spec covers more than enough. We have less stable libraries and implementation-specific interfaces to cover the rest. We are still pretty conservative with those, too. Clojure doesn't have a spec at all (it has documentation for the single implementation; it seems to have another implementation which is even more a toy since no real Clojure programs can run on it, because JVM is part of Clojure, really...) and once things are documented they either stick forever or break. I wouldn't say Clojure has warts; it has massive oozing injuries that keep on spreading indefinitely. So yeah, we'd rather live with the warts from the 90s, the 80s, the 70s, and the 60s. We like our tradition, and we like our programs not breaking. We have a stable base that we can build 2020, 2030, 2040 stuff on.
- auganov 6y agoThese two examples are pretty different unless I'm not getting something? The equivalent of the first in Clojure would be (assuming it's js): (doseq [i (range 3)] (x i) (y i) (z i)) And the equivalent of the Clojure in the second example in js (assuming we had range()) would be: range(3).map(i=>z(y(x(i)))) If you're using a map as a for loop in js you wouldn't have much indication of that either. And no idea how to properly indent these applications. Like this perhaps? range(3).map( i=> z( y( x(i)))) IMO single line is better with x,y,z. But perhaps with longer function names that's what you would do.
- roenxi 6y agoYou are correct. If it helps you can imagine that the functions have side effects and always evaluate to their input. Maybe think of it this way: xxxxx { xx xx xx { xx xx } } vs (xxx xx (xx xx xx xx) xx) I can make a pretty safe guess that the first example is implementing something not worse than O(n^2) before accounting for what the individual calls do, or maybe there is some conditional branching. I can make some guesses about how the code works with probably fair accuracy. That would survive even if I took away the {} braces. In the second example there is not a lot that can be deduced. Whether that matters to you influences whether you are happy coding in a lisp. I prefer the lisps, but there are less hints from the shape of the code.
- auganov 6y agoHmmm, well, so if we're talking purely about indentation wrt control structures, I can see the point, maybe. But if we're talking about the ability to eyeball what's going on, I'm not sure... In Clojure if you see a macro (will be highlighted as a different color) followed by something in square brackets you'll know what's up. The little extra syntax Clojure introduced is quite a big game changer to readability vs other lisps. The problem is we can introduce new macros that may behave differently. You don't have that problem in languages without macros. But if you did have macros, brackets and indentation would lose signal too. Conversely if we didn't have user macros in Clojure the standard convention would be a 100% reliable signal too. I mean, I agree in general that people may find C-like languages more readable, but these examples seem rather poor. I can see why some people would find this more readable if (a || (b && (c + 10 == 11))) { doSomething(); } than this (though I still believe this is mostly a cultural difference) (if (or a (and b (= (+ c 10) 11))) (do-something)) But this is about extra syntax not indentation. You can indent both in many different ways. If anything, given the extra syntax, the js snippet has more possible indentations in this case.
- reikonomusha 6y agoIndenting is absolutely, positively not a problem at all in Lisp.
- chriswarbo 6y agoYour example is a bit apples-to-oranges, in a few ways. First your examples are using different constructs, as well as different languages. It's perfectly reasonable and "lispy" to write a for loop like your first example, e.g. (this is pretty much the same as the first example in https://docs.racket-lang.org/reference/for.html https://docs.racket-lang.org/reference/for.html ) (for ((i '(1 2 3))) (x i) (y i) (z i)) Likewise Lisps don't have a monopoly on factoring common functionality out into helper functions like 'map', e.g. a C-like equivalent to your second example is not at all unusual: map( seq(x, y, z), range(3)) (I've replaced the symbolic name '->' with a made up non-symbolic name 'seq', to do the same job of applying multiple functions to an argument). Secondly you bring up "off-side rule" indentation, like Python, without mentioning I-expressions or their equivalents. These can be used by all Lisps (and anything else based on s-expressions, like DSSSL, since they're trivially convertable to/from s-expressions. Some Lisps also have native support (e.g. they're standardised in Scheme https://srfi.schemers.org/srfi-49/srfi-49.html https://srfi.schemers.org/srfi-49/srfi-49.html ). > map/reduce/recur/let/etc are special I agree for 'let', and it already is a "special form" in most Lisps. I disagree for 'map' and 'reduce', since they are ordinary function calls that are not special in any way (that's part of the appeal for functional programming and macros: that we're not stuck with a finite number of first-order "control structures"). I've not encountered "recur" before, I assume it's a Clojure workaround for JVM tail-call deficiencies? A quick Google shows that it's also a "special form", like 'let'.
- kazinator 6y agoConsider: ((foo() || bar()) && baz() && (xyzzy || plop) && blah versus: (and (or (foo) (bar)) (baz) (or xyzzy plop) blah) You're barking up completely the wrong tree here in your search for Lisp problems. For Lisp, you have a simple algorithm with a few cases in it for indenting, which works equally well if you've broken every token out into a separate line, or just have a few line breaks. There are no concerns like does this punctuator or operator "cuddle" into the previous line, or start the next line? Fewer aspects are a matter of taste, leading to better consistency from one project to the next. Let's compare the coding style of the TXR Lisp standard library to randomly selected code written not only in completely different projects by different people, but in different dialects and languages: TXR Lisp stdlib: http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/tagbody.tl http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/tagbod... Clojure core: https://github.com/clojure/clojure/blob/master/src/clj/clojure/genclass.clj https://github.com/clojure/clojure/blob/master/src/clj/cloju... GUIX internals (Scheme): https://git.savannah.gnu.org/cgit/guix.git/tree/guix/git.scm https://git.savannah.gnu.org/cgit/guix.git/tree/guix/git.scm SBCL compiler code (Common Lisp): https://github.com/sbcl/sbcl/blob/master/src/compiler/dce.lisp https://github.com/sbcl/sbcl/blob/master/src/compiler/dce.li... By golly, it all looks the same, formatting-wise, wherever I look! I could throw any of that code into a stock installation of Vim with no plugins, and easily maintain its style with the built-in Lisp support. That Vim could be an old version from like 1999.
- sundbry 6y agoMap is not a control structure, it is an expression.