This page has been moved to the Programmer's Wiki.
Please do not make any changes or additions to this page. Any modifications, if still appropriate, should be made on Q at the Programmer's Wiki. |
Summary[]
Q is a functional programming language based on term rewriting. Thus, a Q
program or "script" is simply a collection of equations which are used to
evaluate expressions in a symbolic fashion. The equations establish algebraic
identities and are interpreted as rewriting rules in order to reduce
expressions to "normal forms". For instance, here is how you define a function
sqr
which squares its argument by multiplying it with itself:
sqr X = X*X;
Note that, as in Prolog, capitalized identifiers are used to indicate the variables in an equation, which are bound to the actual values when an equation is applied. Equations may also include a condition part, as in the following definition of the factorial function:
fact N = N*fact (N-1) if N>0; = 1 otherwise;
Functions on structured arguments are defined by "pattern matching". E.g.,
the product of a list (denoted in Prolog-like syntax) can be computed with
these two equations:
prod [] = 1; prod [X|Xs] = X*prod Xs;
With this definition, the factorial can now also be defined as follows (the
notation [1..N]
, as in Haskell, denotes an arithmetic
sequence):
fact N = prod [1..N];
As you can see, the definitions are really just like mathematical equations. The syntax is superficially similar to other modern functional languages like Miranda and Haskell, except that Q is "free-format", i.e., it does not use layout to indicate syntactical structure (thus the semicolon is used to terminate an equation).
Due to its term rewriting heritage, Q goes well beyond most other
functional languages in that it also allows you to perform computations with
symbolic expressions. For instance, with the definition of the
sqr
function from above, you will find that sqr
(X+1)
evaluates to (X+1)*(X+1)
. This might first look like
an arcane feature, but it is actually quite useful, because you can try your
definitions with symbolic inputs, too.