

Jan 21 12:34 1992  Q: A Uniform Language for Writing and Running Programs Page 1



What is Q?						    Running programs
----------						    ----------------

* A very high-level programming language		      # Search source files for PATTERN
							      grep $(PATTERN) *.([Cch] cc)
* Multiple data types (like Lisp):
  Numbers, vectors, strings, 1st-class functions, ...	    is macro-expanded (at compile time) into:

* Abstract sequences used for all repetition.		      run_program "grep" (
  (Like APL's use of arrays, or Smalltalk collections.)		glob_expand_list [PATTERN "*.[Cch]" "*.cc")
  Sequences can be stored, or calculated on demand.
							    A prefixed argument is evaluated and passed as
* Powerful operations used uniformly in multiple domains.   stdin to the program.

* Syntax is "medium terse" and designed for interaction.    Converting strings to upper-case (inefficiently):

* "Mostly-functional" (encourages side-effect-free	      :(upper_case :arg) = arg run /bin/tr a-z A-Z
   computation, like Lisp).
							      printf "<%#s>\n" (upper_case "Hello World!\n")
* Builtin shell-style features				      ==> <"HELLO WORLD!\n">
  (e.g. running programs, globbing).

							    Application:  Renaming files using patterns
Lazy vectors						    ---------------------------------------
------------
							    :(__ren :src :dst)=(
{f(A?)}  - a sequence whose i'th element is f(A i)	      :X=(glob src)
							      {run mv $(X?) $(X? match $src $dst)} do)
Elements are calculated on demand.			    :(macro ren :args@) = parse "__ren (quote " args@ ")@"
Each element is only calculated once, hence
must be saved (memoized) (unless optimized).		    Example:  ren *.c.BAK BAK/*.c

Replaces many kinds of loops.				    Macro-expands to:  __ren "*.c.BAK" "BAK/*.c"

Example: Fibonacci function.				    ':X=(glob src)' might set X to ["main.c.BAK" "misc.c.BAK"]
Standard recursive definition:
							    '{ ... } do' loop, doing echo '...' until X is exhausted.
  :(Fib :i)=if i < 2 => 1 || Fib(i-1) + Fib(i-2)
							    For each element of X (say "main.c.BAK") translate it
Very slow (exponential run time).			    (using 'match') into (say "BAK/main.c"),
							    and use the "mv" command to rename it.
Use a lazy vector as a cache:

  :(Fib :i)=if i < 2 => 1 || FibCache(i-1) + FibCache(i-2)  Implementation
  :FibCache = {Fib ?}					    --------------

Fast (linear because the {\tt FibCache} lazy vector	    * Written in C++.
remembers calculated {\tt Fib} values, so they don't	      Most types inherit from "Root" class.
need to be re-calculated).
  (Fib 200) ==>						    * Interpreter for interactive use,
  453973694165307953197296969697410619233826		      or compile into C++ (both share front end).

							    * Multi-language run-time system.  (Incomplete Scheme
Uniformity						      and Common Lisp implementations included.)
----------
							    * Q objects are C++ objects.
Indexing:						      Trivial to add new Q or Lisp builtins written in C++.
  [3 4 5] i  ==>  i'th element of vector
  "abcd" i  ==>  i'the character of string		    * The Q version of a shell command line will generally
  (0 to 100) i  ==> i					    use Unix primitives (forks, pipes, ...) as efficiently
  ./Makefile i  ==>  i'th character in file		    as a shell (avoids needless copying).
  {A? + 100} i  ==> (A i)+100
							    Availabilty
Length of sequence:					    -----------
  size [3 4 5]  ==> 3
  size "abcd"  ==> 4					    * Freely re-distributed (some GNU code used).
  size (0 to 100) ==> 100
  size ./Makefile  ==> # of bytes in file Makefile	    * Pre-alpha; Currently hackers only please!
  size {A? + 100}  ==>  size A
							    * See cygnus.com:~ftp/pub/Q.README for directions.
An (open) file == iterator over a sequence of characters.
							    * Not supported by Cygnus Support.
A Unix executable program is a function
(with a different calling convention).

Standard input and output are abstractly strings.			    Per Bothner
Files in a Unix file system are also abstractly strings.
A pipeline is a special case of function composition.		    (Cygnus Support)	(bothner@cygnus.com)






