Q 7.3 review

Download
by rbytes.net on

Thus, a Q program or "script" is simply a collection of equations which are used to evaluate expressions in a symbolic fashion

License: GPL (GNU General Public License)
File size: 0K
Developer: Albert Graef
0 stars award from rbytes.net

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.

Functional Programming Power

Despite its conceptual simplicity, Q is a full-featured functional programming language with a modern syntax, curried function applications (which implies that functions are first-class citizens which can be parameters and results of function applications), dynamic typing (using a single-inheritance OO type system, akin to Smalltalk), exception handling, and POSIX multithreading. Scripts are executed in a bytecode interpreter which byte-compiles scripts in an eye blink and runs them about as fast as interpreted Lisp or Haskell. The interpreter provides a built-in symbolic debugger which allows you to trace the reductions performed during expression evaluation.

Just like other modern functional languages, Q supports a terse and abstract mathematical style, and it is not uncommon that the equivalent of dozens of lines of C is just a one- or two-liner in Q. Even more important than raw code size is the high level of abstraction which enables you to handle complex data structures with ease, and tackle complicated programming tasks which are almost hopeless endeavours with conventional programming languages.

That is not to say that you won't ever write a line of C again. C has its uses for interfacing to third-party software and for time-critical processing. Like most scripting languages, Q also has a C interface. The Q interpreter is both extensible (new primitives written in C/C++ can be loaded at runtime) and embeddable (Q can be called inside your C/C++ applications). The distribution already includes a number of modules for interfacing to various popular third-party libraries, which makes Q a powerful tool for scientific programming, computer music, multimedia, and other advanced applications. As of version 6.0, Q now also supports SWIG which makes it easy to interface to other libraries if needed.

Using

Using Q is supposed to be fairly simple: you throw together some equations, start the interpreter and then type in the expressions you wish to evaluate. All this can be done with a few keystrokes, using "Q mode" in GNU Emacs/XEmacs. The distribution also includes a syntax file to enjoy syntax highlighting for the Q language in the advanced KDE editor Kate, and the Windows version comes with a graphical application called "Qpad" for editing and running Q scripts. Of course, you can also run the Q programming utilities from the command line if you prefer that.

What's New in This Release:
This release fixes a critical bug related to the new memoization feature introduced in Q 7.1, which could cause random expressions to remain unevaluated.
Some other bugs in the bytecode compiler and the interpreter were also fixed (see the ChangeLog for details). In particular, left-hand sides of equations may now contain const values in the head, provided that they are followed by extra arguments.

Q 7.3 keywords