Tuesday, October 27, 2009

Clojure Syntax

Not many languages are simple enough to have their entire syntax conveniently summarized in a table that fits on one screen. Luckily, Clojure is. The following is a summary put together based on the Clojure reader documentation. I'm a fan of tabular data, so I put this together for my own personal reference.

Reader forms
Symbolsatom, foo-bar, *foo*, etc.
Literals42, "foo", nil, true, false, \c, :foo
Keywords:foo (like symbols, prefixed with colon)
Lists(a b c)
Vectors[a b c]
Maps (hashes){:a 1 :b 2} or {:a 1, :b 2}
Sets#{:a :b :c}
Macro Characters
Quote'form
Character\a \b \c
Comment; txt
Meta^a ^b ^c
Deref@form
Syntax-quote` (backtick)
Unquote~ (tilde)
Unquote-splicing~@ (tilde at)
Macro Characters (Dispatch)
Sets#{:a :b :c}
Regexp#"pattern"
Metadata#^{:a 1 :b 2} [1 2 3] or #^Type x
Var-quote#'x
Anonymous function#(fn [args] (...))
Ignore next form#_

3 comments:

  1. no operators no statements no reserved words...who said that the parentheses are ugly??

    ReplyDelete
  2. A few things:

    - missing namespaced keywords, e.g., ::foo

    - missing the & used to indicate a vararg, e.g., [foo & more]

    - using fn [args] to start an anonymous function, instead of just #(...)

    - missing the anonymous variables, e.g., %1, %2, %&

    ReplyDelete