What is the difference between [ ] and ( ) brackets in Racket (lisp programming language)?
Asked Answered
C

1

32

It seems to me that technically both are interchangeable but have different conventional meanings.

Celestyn answered 1/1, 2017 at 18:26 Comment(0)
M
40

According to the Racket documentation, there is no difference -- there is only a convention to use [ and ] for cond clauses (and use your judgement for the rest, as far as I understand):

The use of square brackets for cond clauses is a convention. In Racket, parentheses and square brackets are actually interchangeable, as long as ( is matched with ) and [ is matched with ]. Using square brackets in a few key places makes Racket code even more readable.

Without having any knowledge about the design of the Racket language, my guess would be that square brackets were introduced as a response to complaints that many Lisp expressions are hard to read due to the large number of identical-looking parentheses, especially at the ends of deeply nested constructs. In other words, it's probably used to allow your eye to easily establish some points of reference in the code to identify what bracket you're closing at any given point.

Masker answered 1/1, 2017 at 18:36 Comment(1)
{ and } are also another equivalent to parentheses, but encouraged for specific use cases (like prefab struct literals). More information about the Reader Syntax.Jibheaded

© 2022 - 2024 — McMap. All rights reserved.