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:

    There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring ‘em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.
    Bernard Mandeville (1670–1733)

    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)

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)