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?
FORMAT
control strings.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
.