The language is composed of comments, primitives, lists, functions, special syntax forms, and a few other special types.
Comments: Start with a semicolon and ends at the end of the same line.
Data types:
- Numbers - a standard JavaScript number type (floating point)
- Strings - a sequence of characters enclosed by double quotes, multiline, escape char sequences: \\ \" \n \r \t
- Symbols - a sequence of characters not containing a whitespace, apostrophe, parentheses, double quote, or semicolon; values can be bound to symbols in an execution environment
- Lists - a sequence of other data types enclosed in parentheses
- Functions - is first class, defining a function creates a closure aware of its lexical environment
- Variables - the only mutable objects (except for those of JavaScript) for controlled mutation and equality comparisons
Special syntax forms:
- (quote expr) - prevents evaluation of expr
- (fn () body) - defines a function with no parameters
- (fn (arg) body) - defines a function with one parameter
- (fn (arg1 arg2) body) - defines a function with two parameters (any number of parameters can be used)
- (fn (arg1 : args) body) - defines a function with at least one parameter, the rest being collected to a list (any number of parameters in front of : args can be used)
- (fn args body) - defines a function with any number of parameters collected to a list
- (if test body-true) - conditional expression; returns 'undefined' if test is false
- (if test body-true body-false) - conditional expression
- (def symbol expr) - binds a value of expr (evaluates it) to symbol in the current environment
- (def symbol1 expr1 symbol2 expr2) - multiple bind (any number of symbol-expr pairs can be used, bindings are processed sequentially in the same environment)
- (do expr1 expr2) - evaluates sequentially all expressions and returns the result of the last one
- (use module) - imports all properties of module object as bindings in the current environment; used for passing environments from JavaScript
- (use module1 module2) - imports two modules (any number can be used)
Default global bindings (immutable):
- nil - JavaScript undefined
- nan - JavaScript NaN
- true - JavaScript true
- false - JavaScript false
- string? - one-parameter function testing the parameter for string
- fn? - one-parameter function testing the parameter for function
- symbol? - one-parameter function testing the parameter for symbol
- cons - two-parameter function constructing a list with the first parameter as a list head and the second parameter as a list tail
- cons? - one-parameter function testing the parameter for list (or rather a cons cell that is a list building block)
- head - one-parameter function taking a list and returning its first item
- tail - one-parameter function taking a list and returning it without its first item
- list - function taking any number of arguments and returning a list of them
- var - one-parameter function that creates a variable
- var? - one-parameter function testing the parameter for variable
- deref - one-parameter function that extracts a value from a variable
- swap! - function that takes a variable and a value and assigns the value to the variable and returns the old value
- apply - takes a function and a list, and applies the function to the items of the list
- filter - takes a function and a list, and applies the function to the items of the list returning a list of items for which the function returned true
- map - takes a function and a list, and applies the function to the items of the list returning a new list of the results (accepts more lists in which case it applies the function to all corresponding items)
- + * - sum and multiplication functions taking any number of parameters
- - - one-parameter negation or two-parameter subtract function
- / - two-parameter division functions
- remainder - two-parameter remainder functions
- < > <= >= - comparison functions taking any number of parameters
- = - two-parameter function comparing for structural equality; only variables are compared by identity
- identical? - two-parameter function comparing arguments for identity
- schedule - function that takes a function and a number, schedules the function to be run in the specified number of milliseconds, and returns an id of the created timeout (like setTimeout in JavaScript)
- unschedule - function that takes a timeout id and removes the scheduled function (like clearTimeout in JavaScript)
- array - function that takes a list and returns an array containing all the list items
- . - function that takes a JavaScript object and a property name (a symbol or string) and returns the value of the property
- .apply - takes a JavaScript object, a property name (a symbol or string), and a list and applies the property value (a function) to the items of the list
- .call - takes a JavaScript object, a property name (a symbol or string), and any number of additional parameters and applies the property value (a function) to the parameters
- set! - function that takes a JavaScript object a property name (a symbol or string) and a value and sets the property of the object to the value; this is a dangerous operation that can compromise the page security if a sensitive object is passed to the script
For interactive examples see Interactive exampes.
For examples of interaction between Cofy and JavaScript see JavaScript integration.