Lisp/Syntax

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Syntax

Comments

Comments start with ;.

By convention:

;;;; File header comment
;;; Paragraph comment-- applies to the large section of code below.
  ;; Applies to code that follows.
  (a b c) ; Applies to this line only

Literals

Numbers

Literal Meaning
123, +123, -123 Integer.
123/456 Rational number.
123.456, 123.456e78 Float.
123.456d78 Double Float.

Strings

Strings are a sequence of bytes. They are enclosed in double quotes. The '\' escapes the next character so it loses its usual meaning. If you want tabs or newlines, insert them without escaping.

"abc\d\"efg\\" -> a, b, c, d, ", e, f, g, \

Symbols

Symbols are identifiers. They can represent a variable, a function, or something else, depending on how they end up being used. Pretty much anything is allowed, unless:

  • The symbol looks like a number
  • Consists only of periods.
  • Can't have anyone of: ( ) ' " ` , : ; \ |

You can escape the 10 special chars with a backslash \, or by placing them between a pair of |.

Note that symbols are stored in uppercase, unless the characters were escaped with \ or a pair of |.

All of the following are the same as FOO:

  • foo
  • FOO
  • Foo

However, these are different:

  • \f\o\o
  • |foo|

Note that all symbols found are stored in a symbol table. That means that everywhere you see 'foo', it will be referring to the same symbol.

Conventions

  • *global-variable*
  • +constant-variable+
  • always-hyphenate
  •  %low-level-function
  •  %%super-low-level-function
  • char->string (converts char to a string)
  • char= (Tests if two chars are equal)

Special Symbols

T is T.

NIL is () is NIL.

S-Expressions

Expression Meaning
(...) This is a function/macro call. The first item is the function name, and the rest are the parameters. Use ':argname' to name parameters.
'atom or '(...) Don't evaluate--just pass it along.
`(...) A literal list, with some elements evaluated. , will be evaluated. ,@ will be evaluated and the result spliced in. Use when you would like to ' everything but a few things.
,atom or ,(...) Something you want to evaluate in `(...).
,@atom or ,@(...) Something you want to evaluate and splice in `(...).
#'funcname Returns the function that funcname represents. (Normally, the value of funcname would be returned.)
#'(lambda (...) (...)) Creates and returns an anonymous function, defined like defun.

Macros

Macros are applied after compiling and before evaluation. Macros look just like functions except their bodies are indented 2 spaces, not 4.