Q (equational Programming Language) - Examples

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++) || fail if safe (I,J) P; = search N I (J+1) P if JA 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\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;

Read more about this topic:  Q (equational Programming Language)

Famous quotes containing the word examples:

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

    It is hardly to be believed how spiritual reflections when mixed with a little physics can hold people’s attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.
    —G.C. (Georg Christoph)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)