- Q (programming language)
Infobox programming language
name = Q
logo =
paradigm = multiparadigm: functional, object-oriented
year =
designer =Albert Gräf
developer =
latest release version = 7.5
latest release date =September 12 ,2006
typing = dynamic, strong
implementations = Q
dialects =
influenced_by =
influenced =Q stands for equational programming language. It is an interpreted, interactive functional
programming language created by Albert Gräf at theUniversity of Mainz inGermany . Q programs are just collections ofequation s which are used to evaluate expressions in a symbolic fashion. Q has many similarities with other modern functional programming languages like Haskell and ML, but is based on generalterm rewriting (a method of computation also used by theMathematica computer algebra system ) instead of thelambda calculus .Despite its conceptual simplicity, Q is a full-featured functional programming language with a modern syntax,
currying ,dynamic typing using anobject-oriented type system,exception handling ,POSIX multithreading, a comprehensive standard library, and an interface to the C programming language. Q is an impure functional language (i.e., operations with side-effects are permitted) with a defaulteager evaluation strategy; "special forms" can be used to implementdata structure s and operations featuringlazy evaluation . Q has been ported to a variety ofoperating system s, includingBeOS ,FreeBSD ,Linux ,Mac OS X , Solaris andMicrosoft Windows . The interpreter isfree software distributed under theGNU General Public License .Various add-on modules are provided for interfacing, e.g., to
GNU Octave ,OpenDX (IBM 's scientific visualization software),Tcl /Tk and ODBC. A graph editor and library is also available. This turns the language into a practical tool for scientific and other advanced applications. Q also comes with an extensive system interface (though not as comprehensive as the facilities provided by other scripting languages such asPerl and Python). Moreover,computer music applications are supported via portable interfaces forMIDI anddigital audio programming.Examples
A "
hello world " example:hello = writes "Hello, world! ";
The following function generates the "stream" (a.k.a. infinite list) of all
prime number s:primes = sieve (ints 2); ints N = bin N (ints (N+1)); sieve (bin X Xs) = bin X (sieve (filter (ndivby X) Xs)); ndivby M N = N mod M <> 0;
An algorithm to solve the "N queens" problem, using backtracking:
queens N = search N 1 1 [] ; search N I J P = write P || writes " " if I>N; = search N (I+1) 1 (P++ [(I,J)] ) || fail if safe (I,J) P; = search N I (J+1) P if J
A tiny system programming example (fetch a file from a WWW server over a socket): /* make sure SIGPIPE (broken connection signal) is ignored */ def _ = trap SIG_IGN SIGPIPE; /* fetch a file from an http server (port 80) */ http HOST NAME = close FD || bstr REPLY where FD:Int = socket AF_INET SOCK_STREAM 0, _ = connect FD (HOST,80), _ = send FD 0 (bytestr (sprintf "GET %s " NAME)), REPLY = recv_loop FD (bytestr ""); /* read data in 64K chunks */ recv_loop FD S = recv_loop FD (S++T) if #T>0 where T:ByteStr = recv FD MSG_WAITALL (64*1024); = S otherwise;
External links
* [http://q-lang.sourceforge.net/ Q-lang homepage]
Wikimedia Foundation. 2010.