Why is it customary to put many closing parentheses on one line in Lisp-based languages?

后端 未结 8 1164
野的像风
野的像风 2021-02-05 01:19

Usually code looks like this:

(one-thing
    (another-thing arg1 (f arg5 r))
    (another-thing arg1 (f arg5 r)))

Why doesn\'t it like this?:

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 01:45

    In Lisp and other languages that use S-expressions for syntax, the parentheses are primarily for the benefit of the compiler, while layout and indentation (which are ignored by the compiler) are for the benefit of programmers.

    So there is no need to put closing parentheses on their own lines: well-chosen line breaks and indentation will be sufficient to make the structure clear.

    For example,

    (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord)
      "Like `clone-indirect-buffer' but display in another window."
      (interactive
       (progn
         (if (get major-mode 'no-clone-indirect)
             (error "Cannot indirectly clone a buffer in %s mode" mode-name))
         (list (if current-prefix-arg
                   (read-buffer "Name of indirect buffer: " (current-buffer)))
               t)))
      (let ((pop-up-windows t))
        (clone-indirect-buffer newname display-flag norecord)))
    

    The structure is clear (to a moderately experienced Lisp programmer) from the indentation. Nothing would be added by bringing some of the closing parentheses down onto new lines:

    (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord)
      "Like `clone-indirect-buffer' but display in another window."
      (interactive
       (progn
         (if (get major-mode 'no-clone-indirect)
             (error "Cannot indirectly clone a buffer in %s mode" mode-name)
           )
         (list (if current-prefix-arg
                   (read-buffer "Name of indirect buffer: " (current-buffer))
                 )
               t)
         )
       )
      (let ((pop-up-windows t))
        (clone-indirect-buffer newname display-flag norecord)
        )
      )
    

    I should add that nearly all Lisp programmers use an editor that displays matching parentheses, performs automatic indentation, and provides a user interface for working direcly with balanced expressions. In Emacs, for example, there's M-( for inserting a new expression, M-) for moving past the end of the current expression, C-M-k for deleting the expression after point, and so on.

    So Lisp programmers never have to count parentheses by hand in order to figure out which ones match.


    Taylor R. Campbell eloquently expresses this rationale:

    The actual bracket characters are simply lexical tokens to which little significance should be assigned. Lisp programmers do not examine the brackets individually, or, Azathoth forbid, count brackets; instead they view the higher-level structures expressed in the program, especially as presented by the indentation. Lisp is not about writing a sequence of serial instructions; it is about building complex structures by summing parts. The composition of complex structures from parts is the focus of Lisp programs, and it should be readily apparent from the Lisp code. Placing brackets haphazardly about the presentation is jarring to a Lisp programmer, who otherwise would not even have seen them for the most part.

提交回复
热议问题