"SfR Fresh" - the SfR Freeware/Shareware Archive 
For instance, here is a little Q script featuring a recursive definition of the well-known factorial function:
fact 0 = 1;
fact N = N*fact(N-1) if N>0;
This definition tells the interpreter that the term (function application) ‘fact 0’ should evaluate to the integer constant 1, while any other term ‘fact N’ with N>0 evaluates to the value of the expression N*fact(N-1).
A closer description of the language is well outside the scope of this manual page, but you can find some further notes about Q below, and you should also take a look at the Q info file (available online using info qdoc or with the help command of the interpreter) for details and many more examples.
The primary interface to the Q language is the interpreter program q. The qc program is a compiler for Q scripts which is usually invoked automatically by the interpreter to translate the source script to a bytecode format which is suitable for efficient execution. To run a script stored in a file foo.q you usually invoke the interpreter just as:
q foo.q
(The script name can also be followed by other parameters which are passed to the script and can be accessed through the built-in ARGS variable of the interpreter.)
You can also execute compiler and interpreter separately, like this:
qc foo.q
q q.out
The compiler will then compile the source script foo.q to the bytecode file q.out which can be loaded by the interpreter. Note that if you run a source script through the interpreter, then the compilation step is handled automatically and the bytecode file is removed automatically as soon as it has been loaded by the interpreter.
Both compiler and interpreter can also be invoked without arguments, or with an empty script name, in which case only the built-in functions and definitions in the script prelude.q (which by default includes the standard Q library) are loaded. The automatic inclusion of the prelude script can also be suppressed with the --no-prelude compiler option.
The script name can also be a single hyphen ‘-’ to indicate that the script should be read from standard input.
Script and bytecode files are searched for on the ‘‘Q library path’’ which usually defaults to something like
.:/usr/share/q/lib:/usr/lib/q
You can override this default by setting the QPATH environment variable, by using the -p command line option, and with the path command of the interpreter.
On the interactive command line, the value of the last expression can be referred to using the ‘‘anonymous’’ variable, denoted by an underscore (‘_’). Moreover, the interpreter understands a number of special commands which allow you to define variables, inspect and adjust various system parameters, edit and run scripts and command source files, read online info, load and save variables values, etc. Please refer to the Q info page for a description of these. You can also put such commands into the .qinitrc and .qexitrc files in your home directory which are sourced when the interpreter starts up and is exited in interactive mode, respectively. This provides a convenient means, e.g., to customize parameters of the interpreter according to your taste, and to automatically reload and save variable values.
On UNIX systems, you can also run Q scripts directly from the shell using the ‘‘shebang’’ #! to specify the q program as a command language processor. For instance, use the following as the first line of your script to invoke q with the option -cfoo which causes the function foo to be evaluated at startup:
#!/usr/local/bin/q -cfoo
Such lines will be treated as comments by the compiler and interpreter. It is also possible to specify compiler and interpreter options at the beginning of the main script using the notation ‘#! option’. For instance:
#!/usr/local/bin/q
#! -w
#! -cfoo
Instead of directly running the script file, you can also use the qcwrap program to translate the script to a C file. This is useful if your shell does not support the #! notation, or if the script is to be distributed in a self-contained, binary form. The qcwrap program is available as an optional addon, see the Q info file for details.
The Q interpreter is extensible using ‘‘external’’ modules written in C or C++ (which are loaded at runtime, if possible), and can itself be used as an extension language for other C/C++ applications. Q has a fairly complete POSIX system interface and a comprehensive collection of addon modules which interface to various popular open source software packages including, e.g., GNU Octave, various GUI, graphics, multimedia, database and web-related libraries, and a module for the Apache web server. There is a language mode for emacs, which provides a convenient environment for editing and running Q scripts, and syntax files for the vim and kate text editors are also available. All this is described in much more detail in the Q info file and in the other documentation available on the Q website.
The Q interpreter uses a special pattern-matching technique to determine matching equations quickly and during a single left-to-right scan of each potential redex. This usually works very well, but there are some pathological configurations of left-hand sides of equations which cause an exponential blow-up of the tables of the pattern-matching automaton; fortunately, they are rare. You can tell that you have run into such a situation when the interpreter needs a long time to start up or appears to hang during bytecode compilation. The only way around this currently is to rewrite your script so that the amount of overlap between equations is reduced.
Another limitation of the current implementation is that special argument patterns and paths to left-hand side variables are currently encoded as bit vectors to save memory space. Thus functions cannot be declared with more than 32 special parameters, and the left-hand side of a rule or local variable definition may not be more than 32 levels deep. There are also some hardcoded limits in the compiler for the sizes of the expression and code table for a single rule. The default table sizes are fairly large and, so far, this has never been a problem in practice. If you do run into an ‘‘expression too complex’’ or ‘‘code table overflow’’ error then it is probably time to restructure your program anyway. ;-)
http://q-lang.sourceforge.net