[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Q is an interactive programming language in the tradition of Lisp and APL:
Q1> 3+4 # This is a comment 7 |
Q1>
is used
to indicate user input; the next line is the output.
Alternatively:
3+4 ==> 7 |
3+4
evaluates to 7
.
Q has APL-like features for manipulating arrays:
(1 upto 4) + [40 30 20 10] ==> [41 32 23 14] |
[]
is used to construct one-dimensional arrays.)
Q is based on one-dimensional sequences,
in contrast to APL's multi-dimensional arrays.
(Arrays are viewed as nested sequences.)
A colon before an identifier indicates a declaration:
Q3> :a=[10 20] Q4> a+a 20 40 |
Q3
, a
is declared as a new variable.
Note that the =
is not assignment, it is unification
(bi-directional pattern matching, as in logic programming):
Q5> [:x :y] = a Q6> sprintf "x is %s, y is %s.\n" x y x is 10, y is 20. |
sprintf
is similar
to the C function (except that its arguments are
dynamically typed, and the output is to a dynamically
allocated string); printf
writes to a file.
Either supports many of the features
of Common Lisp's format
.
String literals can contain C-style escapes (such as \n
).
They can also contain an expression following a $
,
which cancels one level of quotation.
If a
has the value "5"
then
"$(3+a)"
evaluates to the string "8"
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function definitions look like the following:
Q7> :(Factorial :x) = if x=0 => 1 || x * (Factorial x-1) Q8> 2 / Factorial 20 1/1216451004088320000 |
if cond => alt1 || alt2
is Q's if-then-else syntax.
Note that no parentheses or commas are needed for a function call. Such delimiters are inconvenient for interactive use.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Q uses spaces to avoid parentheses. All of the operations within one word are done before the others:
3+10*10 ==> 103 3+10 * 10 ==> 130 3 + 10*10 ==> 103 |
New-lines separate "statements" (which are just expressions). A new-line is equivalent with `;'. Requiring an explicit statement terminator would be clumsy and error-prone in an interactive language.
Q also uses indentation to express control structure, like Python and Haskell's "off-side" rule.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |