3

I've recently done some digging. Due to my lack of Lisp experience, I want to verify what I've found. Is this a correct characterisation of the behaviours of square brackets in Scheme, Racket, Common Lisp, and Clojure?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
J. Mini
  • 1,868
  • 1
  • 9
  • 38
  • 1
    In Clojure, brackets are also used to specify arguments for functions, for sequential collections destructuring, for `:keys` map destructuring, for bindings of all sorts, for `:require` vectors in the `ns` form. – Eugene Pakhomov Jun 19 '21 at 22:54
  • 1
    Although of course it can be argued that those are all vectors, just ones not available to the user directly in their vector form. – Eugene Pakhomov Jun 19 '21 at 23:03
  • 3
    Square brackets aren't macro characters in Common-Lisp, which means that by default they `READ` as parts of `symbols`. `'([ [ ] [])` is a `LIST` of four peculiar looking `symbols` – pjstirling Jun 20 '21 at 00:03
  • 2
    @EugenePakhomov They're certainly all vectors. `defn` is a macro, and it receives its arguments as a vector. A macro can easily inspect the types of its arguments, so you can confirm that they are real vectors. – amalloy Jun 20 '21 at 02:02

1 Answers1

4

I think for Scheme, you have it right.

In Common Lisp, the standard is not CLtL2, but the ANSI standard, which for all intents and purposes is the same as the CLHS (e. g. at https://clhs.lisp.se). The reader behaviour is defined in section 2. Square brackets by default (i. e. in standard syntax) are constituent characters, so they may be used without special escaping in symbol names. For example, [, [], ][, APPLE-][, >][<, [[[, etc. are all valid symbol names that can be used without escaping. These characters are, however, explicitly reserved for the programmer to use in their own readtables. Several libraries use this, e. g. CLSQL for SQL literals.

In Clojure, square brackets denote vectors, which is a separate kind of collection from e. g. lists. The evaluation of a literal vector in code is to construct a new vector of the evaluated forms inside. Example: [a 'b (+ a b)] evaluates to a new vector of the value of a, the symbol named b, and the sum of the values of a and b. Macros and other special forms generally use vectors for syntax parts that are not to be evaluated as a function call. Examples: (defn foo [a b c] …) — a vector of symbols to be bound as formal parameters in the body of the function definition. (let [a 1 b (+ a forble)] …) — a lexical binding for a and b.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Svante
  • 50,694
  • 11
  • 78
  • 122
  • 1
    So in Scheme brackets are used to increase code readability. In Clojure they are used as a notation to create vectors and to increase code readability. In Common Lisp they are used to provide data structure notations and to embed languages (SQL, ...). Would that be a fair summary? – Rainer Joswig Jun 20 '21 at 20:44
  • 1
    In a way, yes, but only descriptively. There is also a lot of variation in _how_ this is done and _who_ (standard, implementation, library, program) does it. – Svante Jun 20 '21 at 21:00
  • 1
    i love the APPLE-][ example :-) – Damien Mattei Mar 18 '22 at 10:11