Writing Definition Scripts -------------------------- Each line in a definition script (→#.std←, →#.math←, →#.bta←, etc ···) is one of the following ten types: [cm] ⍝ ··· ·· ··· comment line is ignored. [wt] white space line is ignored. [fm] fn(···) fn(···) ··· monadic function declarations. [fl] ← fn(···) fn(···) ··· left-binding dyadic function declarations. [fr] → fn(···) fn(···) ··· right-binding .. .. .. [pv] ··· :: ··· pattern variable definition. [rd] ··· = ··· reduction (simplification) equation. [tc] ··· ≡ ··· traced .. .. .. .. [tr] ⍝ ··· -> ··· test line: test expression and expected result. [te] ⍝ ··· !! ··· test line: .. .. .. error. Details ------- [cm] [wt] Comment and white space lines are ignored. [fl] [fr] [fm]: Monadic and dyadic functions are declared in order of precedence; the earli- er they occur in the script, the tighter they bind. The expression in paren- theses to the right of the function identifier is its definition and is one of: Empty(): function interpretation relies on reduction [rd] rules. An APL function expression, such as (÷), (+/) or (3∘○). A macro definition such as (n m → m | n). Local variables to the left of the colon are bound with corresponding items of the function's argument. Code segments to the right of the colon are then evaluated in the normal way. [pv] Pattern variables are used to reduce the number of reduction rules. Exampl- es include: i j :: ¯1 0 1 ⍝ single digits. See: →#.bta← ¯i ¯j ¯k :: ← ⍝ negative literal numbers. See: →#.math← n m :: I V X L C D M ⍝ Roman numerals. See: →#.roman← See also: →Expression_simplification←. [rd] [tc] Reduction equations simplify expressions. An example might be for Roman numeral arithmetic: I.I.I.I = I.V. Equations are searched top-to-bottom in the script until a match is found, which means that specific equations should precede more general ones. See →Expression_simplification← and →Debugging←. [tr] [te] Test lines are extracted and used by function "test". See: →Overview←. Diamonds -------- In common with APL, the diamond character "⋄" may be used in place of a new line. Note (also in common with APL) that comments are stripped BEFORE diamonds are processed, so in the following, all characters to the right of the left-most "⍝" are ignored: ⌽i = i ⍝ atom ⋄ ⌽ i.j = j.i ⍝ pair In the above, the second code segment will be ignored, as the line will be interpreted: ⌽i = i An example of an appropriate use of a diamond separator might be the combination of the declaration and reduction rule for a simple function: 2nd() ⋄ 2nd(p;q;r) = q ⍝ second item of triple. Example ------- The following short script for counting in the "one, two, plenty" system illust- rates a number of these constructs: display tasman ┌→──────────────────────────────────────┐ │⍝ Native Tasmanian counting. │ │ │ │ ← +() ⍝ addition │ │ ← =() ⍝ reduction │ │ │ │ n m :: one two plenty ⍝ variables│ │ │ │ one + one = two ⍝ reduction│ │ m + n = plenty ⍝ equations│ │ │ │⍝ Test cases: │ │⍝ one + one -> two │ │⍝ one + one + one -> plenty │ │⍝ plenty + plenty -> plenty │ │⍝ two two !! missing operator│ │⍝ plenty + !! missing operand │ └───────────────────────────────────────┘ ⎕←test tasman ⍝ test script. 1 1 1 1 1 tasman script until'' ⍝ interactive session. one + one two one + one + one + one plenty two + one plenty ( In "The Story of Civilization", Vol 1 (Simon and Schuster, New York, 1954) Will Durant says: The Tasmanian tribes counted "Parmery, calabawa, cardia", meaning "one, two, plenty." The Guaranis of Brazil ventured further with: "One, two, three, four, innum- erable." (Although this last claim doesn't quite square with the first ten numbers in Guarani: peteî mokôi mbohapï irundï po poteî pokôi poapï porundï popá. See: http://www.zompist.com/numbers.shtml ) The Cambridge Encyclopedia of Language adds: Andamanese makes do with two number-words one and one-plus. Khosian languag- es express one, two and occasionally three, but rarely more. ) Exercises --------- 1. Write a script to implement addition in the alleged Guarani system. 2. Extend the tasman script with a subtraction function. Zero would be repres- ented by "none", and less than zero by "Eh?". 3. Extend the Guarani script in a similar way. Back to: →Contents←