BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help

Q (programming language)

Print-Friendly
About 2 pages (555 words)

Bookmark and Share Questions on this topic? Just ask!
Q
Paradigm multiparadigm: functional, object-oriented
Designed by Albert Gräf
Typing discipline dynamic, strong
Major implementations Q

Q stands for equational programming language. It is an interpreted, interactive functional computer programming language created by Albert Gräf at the University of Mainz in Germany. Q programs are just collections of equations 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 general term rewriting (a method of computation also used in computer algebra systems) instead of the lambda calculus. Despite its conceptual simplicity, Q is a full-featured functional programming language with a modern syntax, currying, dynamic typing using an object-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 default eager evaluation strategy; "special forms" can be used to implement data structures and operations featuring lazy evaluation. Q has been ported to a variety of operating systems, including BeOS, FreeBSD, Linux, Mac OS X, Solaris and Microsoft Windows. The interpreter is free software distributed under the GNU 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 as Perl and Python). Moreover, computer music applications are supported via portable interfaces for MIDI and digital audio programming.

Examples

A "hello world" example:

hello            = writes "Hello, world!\n";

The following function generates the "stream" (a.k.a. infinite list) of all prime numbers:

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 "\n" 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<N;
                 = () otherwise;

safe (I1,J1) P   = not any (check (I1,J1)) P;

check (I1,J1) (I2,J2)
                 = (I1=I2) or else (J1=J2) or else
                   (I1+J1=I2+J2) or else (I1-J1=I2-J2);

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 a 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\r\n\r\n" 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

View More Summaries on Q (programming language)
 
Ask any question on Q (programming language) and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Q (programming language) from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy