logoalt Hacker News

kazinator • today at 5:43 PM • 0 replies • view on HN

That specific example would always be

  (foo
     bar
     baz
     quux)
Once we separate even the first argument on its own line, we have made clear that that is where the division is, and given up on more detail: it's some kind of operator with arguments.

If the list is quoted then:

  '(foo
    bar
    baz
    quux)
The ambiguity is if you have this:

   (foo bar
     ... )
If foo is a function

  (foo bar 
       baz
       quux)

If it is a macro with a trailing body of forms ("implicit progn" in CL parlance) then we don't know, without the macro definition, how many of the initial arguments are not part of those forms:

  (foo bar
    baz
    quux)
or

  (foo bar
       baz
    quux)

or

   (foo bar
        baz
        quux)
A good solution to all this is parinfer: specifically indent -> parens mode. That is to say, manually or semi-automatically control indent, and let parentheses be inferred.

The above issues are all symptoms of the assumption that the programmer controls parentheses, leaving the indentation to be figured out.

But parentheses are the aspect of the code that can only be one way, whereas indentation has latitudes that mean nothing to the machine but visually indicate semantics.

So, switch roles: let the machine deal with the parentheses. Then there is no problem to be solved whatsoever that requires declaration info or execution of functions.

The downside is that if we have a correct inference from parentheses to indentation, it can help to spot errors. For instance, if you write:

  (pattern-match pattern
                 body1
                 body2)
and the indentation jumps to this:

  (pattern-match pattern
                 body1
    body2)
then you have an instant reminder that you forgot to write the expression to be analyzed by the pattern, causing your first body form to be interpreted as that.

To me, that is minor, and I'm willing to throw it under the bus by controlling indentation.

Parens inference from indentation is not perfect, though! This is illustrated best by cond. If you have a cond clause like this:

   ((condition ...)
      form
      form)
parinfer wants to do this:

   ((condition
      form
      form))
because the forms are indented, they are assumed to be constituents of the condition expression. The fix for that is to just learn to embrace:

   (cond
     ((condition)
      form
      form)
     ((condition)
      form
      form))
Only problem with that is working with your legacy code where you have the indentation.