Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?:

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

It allows adding and removing "another-thing" lines more easily (without removing and re-adding trailing closing parenthesis). Also you can put a some comment on that lone closing parenthesis (such as "; end of the loop").

How bad is it when I mix by code that uses the second style with existing code that uses the first style?

like image 353
Vi. Avatar asked Nov 29 '10 14:11

Vi.


People also ask

What is the purpose of parentheses () in a programming expression?

In many computer programming languages, parentheses have a special purpose. For example, they are frequently used to enclose arguments to functions and methods. In languages such as Lisp, parentheses define an s-expression. In regular expressions, parentheses are used for pattern grouping and capturing.

What language uses parentheses?

The C language pattern of parentheses use has prevailed in most modern languages: ( ) Use for functions. [ ] Use for arrays. { } Use for blocks.

What do brackets mean in C?

In C/C++, they are used to signify the start and end of a series of statements.


1 Answers

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.

like image 179
Gareth Rees Avatar answered Sep 21 '22 12:09

Gareth Rees