Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a correct characterisation of the behaviours of square brackets in Scheme, Racket, Common Lisp, and Clojure?

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?

  • R5RS and R7RS Scheme: Square brackets are reserved in the spec "for possible future extensions to the language" (page 6 for R5RS and page 9 for R7RS), but implementations tend to implement square brackets as R6RS Scheme does below. Racket is one such example, even having community conventions for where to prefer square brackets.
  • R6RS Scheme: "Matched square brackets can be used synonymously with parentheses". In other words, square brackets are valid brackets as long as they match. Alternative source.
  • Common Lisp: Square brackets are reserved so the user can give them meaning via reader macros. They do not have meaning within the standard language, except inside FORMAT control strings.
  • Clojure: Square brackets for vectors and parentheses for lists. These are not the same thing.
like image 669
J. Mini Avatar asked Oct 15 '25 18:10

J. Mini


1 Answers

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.

like image 171
Svante Avatar answered Oct 18 '25 14:10

Svante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!