4 ms·
Coding in Haskell changed the way I indent Lisp code. The way I intent now differs from most Lisp indentation styles in that (a (b (c (d (e (f (g (h xxx
by hollerith 15y ago
Coding in Haskell changed the way I indent Lisp code. The way I intent now differs from most Lisp indentation styles in that
(a (b (c (d (e
(f (g (h xxx yyy (i uuu (vv ww qq) (j
foo bar))))))))))
becomes allowed whereas
(let ((a b)
(c d))
foo)
becomes not allowed -- you have to write it as
(let
((a b)
(c d))
foo)
or (if you need the let clauses to line up) as
(let
(
(a b)
(c d))
foo)
Well, the let form is such a unusual case that I will give a second example:
(f (g xxx
yyy)
zzz)
becomes not allowed -- you have to write it as
(f
(g xxx
yyy)
zzz)
or (if you need g's arguments to line up) as
(f
(g
xxx
yyy)
zzz)
Although I used two spaces for every new level of indentation above, the choice of the number 2 is of course not an essential element of the style.
- deleted 15y ago[deleted]
- spacemanaki 15y agoThat seems pretty strange, and some of those examples are really hard to read. Care to share some real Lisp code and not just (a (b (c ... ))) examples? And maybe some motivating examples in Haskell? I just can't see how any of these are an improvement and I'm curious.
- hollerith 15y agoIn Haskell it is not uncommon to see things like foo x y = x >>= \a -> case a of Leaf -> .. Branch aleft aright -> .. which translates into the Scheme pseudocode (define (foo x y) (bind x (lambda (a) (case a ((leaf) ..) (branch aleft aright) ..) Although such an indentation pattern (namely, multiple "unclosed" open parens on one line, which all get closed at once) is very uncommon in Scheme or Lisp code, I find it handy enough to trade off for certain other "competing" indentation patterns (namely, the one in the final example in grandparent) traditional in Scheme and Lisp. I have been meaning to put my .emacs.d (5800 lines) on Github. Do you want me to email you when I have done so?
- spacemanaki 15y agoThat makes much more sense given the snippet of Haskell and isn't so hard to read. No need to email though, I'm pretty happy with the standard Lisp indentation style. Thanks for clarifying.
- hollerith 15y agoYou are welcome. By the way, any Emacker who shares my skepticism of the value of the traditional Emacs practice of having special indentation amounts for certain special forms and who prefers a slightly more regular/boring indentation style can get the TAB key to reflect that skepticism/preference with (setq lisp-indent-offset 2) and (setq lisp-body-indent 2) (or (setq lisp-indent-offset 4) and (setq lisp-body-indent 4) or such). Disclaimer: untested on Emacs 23 or 24. (I'm still on Emacs 22.)
- dan00 15y agofoo x y = x >>= \a -> case a of Leaf -> .. Branch aleft aright -> .. Hanging lambdas yes, but I don't see the advantage of putting the beginning of the case into the same line. foo x y = x >>= \a -> case a of Leaf -> .. Branch aleft aright -> .. But the example is a bit contrived, because you could write it this way: foo x y = do a <- x case a of Leaf -> .. Branch aleft aright -> ..
- hollerith 15y agoWe must have different goals for our choice of indentation style: my goal is to minimize lines of code while preserving readability. (Not lining things up as in (f xxx yyy) initially detracts from readability, but in my experience with the new style, readability goes back up to very close to where it was as I get used to the new style.)
- eru 15y agoI actually like the first one best, and would prefer a case-lambda even more. Once it becomes available we can write foo x y = x >>= \case Leaf -> .. Branch l r -> .. modulo whatever syntax gets chosen. That would save an unnecessary temporary variable. (See https://groups.google.com/group/haskell-cafe/browse_thread/thread/6f80e46ae3695979/7cb2c5d36c36c7f4 https://groups.google.com/group/haskell-cafe/browse_thread/t...) Temporary variables clutter up the name space. While reading code you have to look for shadowing, or whether that variable is ever used again.
- WildUtah 15y agoDoes that happen because Haskell has significant whitespace? I suppose you might get used to the way it requires you to line things up and come to like it. (let ((a b) (c d)) foo) Seems odd to me because the (a b) and (c d) are semantically the same but can't line up. Or, they can't line up without spending a new line on each function call, as if this were Java or something. Don't worry; I know Haskell isn't Java-like. (Haskell is Python plus Perl: Significant whitespace plus polymorphism based on external context of the expression. Written in the APL charset.)
- hollerith 15y ago>Does that happen because Haskell has significant whitespace? You can do the same thing in Lisp and Scheme, where of course whitespace is not significant. If you're asking if Haskell's significant whitespace somehow made it more probable that programmers would do it, the answer is, I do not know.
- arethuza 15y agoYour comment got me wondering if you could make whitespace significant in Lisp - perhaps via a reader macro....
- dustingetz 15y agoi'm not sure think your personal take on lisp indentation has any compelling benefits to justify departing from consensus. i recognize a lot of old school lispers follow a philosophy of "lisp empowers me so much that i don't need a team", so they do whatever they want and their productivity so godly that nobody says anything, but this isn't particularly helpful to increased lisp adoption, which benefits us all. maybe it isn't harmful either, but we who are already using it aren't in a position to judge this.
- hollerith 15y agoGood points. (I have not yet distributed significant amounts of Lisp code, and if I started, I'd probably stick to a conventional style or make it very clear that the distribution is just a dump of something coded just to scratch my own itch.)
- cheatercheater 15y agoWhat are you talking about, he's using standard banner-style-indent with lisp-close-brackets (i.e. the closing brackets are on the last line of the body, not below the body) I use banner style in all languages and it works well.
- hollerith 15y agoIn that case, if you use Emacs, perhaps you'd like to see the Emacs Lisp that I have bound to my tab key (which however has not been made to work with non-Lisp-syntax languages). If you write "(a (b (c (d (e (f" RET, TAB indents just as many spaces as it would if you had written just "(a" RET.
- cheatercheater 15y agoSounds cool. I don't use emacs at all, but maybe you can post it anyways?
- hollerith 15y ago
- gtani 15y agoFrom what you're presenting here, you're using significantly more vertical than other lispers generally. Why is this preferable to rainbow parens, ShowParenMode or parenFace (there's probably lots of other paren highlighters for emacs), to match open/closing delimiters?
- hollerith 15y agoI disagree that my style uses more vertical: it has one change that increases vertical and one that reduces it, and no one has done an analysis to determine the net effect.
- eru 15y agoWhat's wrong with that? (let ((a b) (c d)) foo) I often write my lets and wheres in Haskell as let a = b c = d in foo and have seen that style in other people's code, too.
- cheatercheater 15y agoinconsequential indent level. Makes it impossible to analyze algorithms at a glance. If you are consequent about your indent levels and get a lot of experience writing code like that, you can glance over a piece of text, notice the main keywords and indent levels, and you immediately have a good idea of what the code might be doing.
- eru 15y agoYou make a good argument for choosing one style and sticking to it, but I don't see how it helps you choose between the different styles here.