rtt=. ,@(N */ N) Ravelled times table aprt=. -.@(N e. rtt) Alternative test and selection (primes do not occur in the times table for N) apsel=. aprt # N B. Coordinate Geometry Do experiments on the vector (or point) p=. 3 4 and the triangle represented by the table tri=. 3 2$ 3 4 6 5 7 2 L=. %:@(+/)@*:"1 Length of vector (See rank in the dictionary or in Lesson 21) LR=.L"1 Length of rows in table disp=. ] - 1&|. Displacement between rows LS=. LR@disp Lengths of sides of figure sp=. -:@(+/)@LS Semiperimeter (try sp tri) H=. %:@(*/)@(sp,sp-LS) Herons formula for area det=. -/ . * Determinant (See dictionary) SA=. det@(,.&0.5) Signed area; negative if plotted vertices are in clockwise order sa=.det@(],.%@!@<:@#) General signed volume; try it on the tetrahedron tet=. ?4 3$9 as well as on the triangle tri . 14: FORMAT A numeric table such as: ]t=.(i.4 5)%3 0 0.333333 0.666667 1 1.33333 1.66667 2 2.33333 2.66667 3 3.33333 3.66667 4 4.33333 4.66667 5 5.33333 5.66667 6 6.33333 can be rendered more readable by formatting it to appear with a specified width for each column, and with a specified number of digits following the decimal point. For example: ]f=. 6.2 ": t 0.00 0.33 0.67 1.00 1.33 1.67 2.00 2.33 2.67 3.00 3.33 3.67 4.00 4.33 4.67 5.00 5.33 5.67 6.00 6.33 The integer part of the left argument of the format function specifies the column width, and the first digit of the fractional part specifies the number of digits to follow the decimal point. Although the formatted table looks much like the original table t, it is a table of characters, not of numbers. For example: $t 4 5 $f 4 30 +/t 10 11.3333 12.6667 14 15.3333 +/f domain error However, the verb do or execute (".) applied to such a character table yields a corresponding numeric table: ". f 0 0.33 0.67 1 1.33 1.67 2 2.33 2.67 3 3.33 3.67 4 4.33 4.67 5 5.33 5.67 6 6.33 3* ". f 0 0.99 2.01 3 3.99 5.01 6 6.99 8.01 9 9.99 11.01 12 12.99 14.01 15 15.99 17.01 18 18.99 Exercises: 14.1 Using the programs defined in Lesson 13, experiment with the following expressions: 5.2 ": d=. %: i.12 5.2 ":,.d fc=. 5.2&":@,.á fc d 20 (fc@h3 ,. h5) d 20 (fc@h3 ,. '|'&,.@h5) d plot=. fc@h3,.'|'&,.@h5 20 plot d 15: PARTITIONS The function sum=. +/ applies to an entire list argument; to compute partial sums or subtotals, it is necessary to apply it to each prefix of the argument. For example: a=. 1 2 3 4 5 6 sum=. +/ sum a sum\ a 21 1 3 6 10 15 21 The symbol \ denotes the prefix adverb, which applies its argument (in this case sum) to each prefix of the eventual argument. The adverb \. applies similarly to suffixes: sum\. a 21 20 18 15 11 6 The monad < simply boxes its arguments, and the verbs <\ and <\. therefore show the effects of partitions with great clarity. For example: <1 2 3 õýýýýýÀ þ1 2 3þ Áýýýýýã (<1),(<1 2),(<1 2 3) õýÃýýýÃýýýýýÀ þ1þ1 2þ1 2 3þ ÁýÂýýýÂýýýýýã <\ a õýÃýýýÃýýýýýÃýýýýýýýÃýýýýýýýýýÃýýýýýýýýýýýÀ þ1þ1 2þ1 2 3þ1 2 3 4þ1 2 3 4 5þ1 2 3 4 5 6þ ÁýÂýýýÂýýýýýÂýýýýýýýÂýýýýýýýýýÂýýýýýýýýýýýã <\. a õýýýýýýýýýýýÃýýýýýýýýýÃýýýýýýýÃýýýýýÃýýýÃýÀ þ1 2 3 4 5 6þ2 3 4 5 6þ3 4 5 6þ4 5 6þ5 6þ6þ ÁýýýýýýýýýýýÂýýýýýýýýýÂýýýýýýýÂýýýýýÂýýýÂýã The oblique adverb /. partitions a table along diagonal lines. Thus: ]t=.1 2 1 */ 1 3 3 1 sum/. t 1 3 3 1 1 5 10 10 5 1 2 6 6 2 10 #. sum/. t 1 3 3 1 161051 121*1331 words p Nobly , nobly , Cape St. Vincent Exercises: 17.1 Choose sentences such as pp=.+//.@(*/) from earlier exercises, enclose them in quotes, and observe the effects of word-formation (;:) upon them. 18: NAMES AND DISPLAYS In addition to the normal names used thus far, there are three further classes: 1) $: is used for self-reference, allowing a verb to be defined recursively without necessarily assigning a name to it; as illustrated in Lesson 23. 2) The names x. and y. are used in explicit definition, discussed in Lesson 19. They denote the arguments used in explicit definition. 3) A name that ends with an underbar ( _ ) is a locative. Names used in a locale F can be referred to in another locale G by using the suffix F in a locative name of the form pqr_F_ , thus avoiding conflict with otherwise identical names in the locale G. See Section H of Part II for further details. The form of the display invoked by entering a name alone is established by 9!:3, as described in the appendix. For example: mean=. +/ % # 9!:3 (4) NB. Tree form mean õý / ýýý + ýýÊý % Áý # 9!:3 (5) NB. Linear form mean +/ % # Multiple displays are also possible: 9!:3 (5 4 2) mean +/ % # õý / ýýý + ýýÊý % Áý # õýýýýýÃýÃýÀ þõýÃýÀþ%þ#þ þþ+þ/þþ þ þ þÁýÂýãþ þ þ ÁýýýýýÂýÂýã Exercises: 18.1 Experiment with the use of locatives. 19: EXPLICIT DEFINITION The sentences in the example: m=. '3 %: y.' d=. 'x.%: y.' that were executed in Lesson 17, can be used with the explicit definitionconjunction : to produce a verb: script=. 0 : 0 3 %: y. : x.%: y. ) roots=. 3 : script roots 27 4096 3 16 4 roots 27 4096 2.27951 8 Adverbs and conjunctions may also be defined explicitly. For example: table=. 1 : 0 [ by ] over x./ Verbs by and over from Lesson 4 ) 2 3 5 ^ table 0 1 2 3 õýÃýýýýýýýýýýÀ þ þ0 1 2 3þ ÈýÊýýýýýýýýýý³ þ2þ1 2 4 8þ þ3þ1 3 9 27þ þ5þ1 5 25 125þ ÁýÂýýýýýýýýýýã Control structures may also be used. For example: f=. 3 : 0 factorial=. 3 : 0 if. y.<0 a=. 1 do. *:y. while. y.>1 else. %:y. do. a=. a*y. end. y.=. y.-1 ) end. a ) f"0 (_4 4) factorial"0 i. 6 16 2 1 1 2 6 24 120 Exercises: 19.1 Experiment with and display the functions roots=. 3 : script and 13 : script (which are equivalent). 19.2 See the discussion of control structures in the dictionary, and use them in defining further verbs. 19.3 Define the following adverb, and experiment with it in expressions such as ! d b=. i.7 d=. 1 : 0 +:@x. ) 19.4 Using the program pol from Exercise 15.2, perform the following experiments and comment on their results: g=. 11 7 5 3 2 & pol e=. 11 0 5 0 2 & pol o=. 0 7 0 3 0 & pol (g = e + o) b=. i.6 (e = e@-) b (o = -@o@-) b ANSWER: The function g is the sum of the functions e and o . Moreover, e is an even function (whose graph is reflected in the vertical axis), and o is an odd function (reflected in the origin). 19.5 Review Section G of Part III and use line texts to make further explicit definitions. 19.6 Enter the following explicit definition of the adverb even and perform the suggested experiments with it, using the functions defined in the preceding exercise: even=. 2 : 0 -:@(x.f. + x.f.@-) ) ge=. g even (e = ge) b (e = e even) b 19.7 Define an adverb odd and use it in the following experiments: exp=. ^ sinh=. 5&o. cosh=. 6&o. (sinh = exp odd) b (sinh = exp .: -) b The primitive odd adverb .: - (cosh = exp even) b (exp = exp even + exp odd) b 19.8 The following experiments involve complex numbers, and should perhaps be ignored by anyone unfamiliar with them: sin=. 1&o. cos=. 2&o. (cos = ^@j. even) b (j.@sin = ^@j. odd) b 20: TACIT EQUIVALENTS Verbs may be defined either explicitly or tacitly. In the case of a one-sentence explicit definition, of either a monadic or dyadic case, the corresponding tacit definition may be obtained by using the adverb 13 : as illustrated below. First enter 9!:3 ] 2 5 so as to obtain both the boxed and linear displays of verbs: s=. 0 : 0 (+/y.) % (#y.) ) mean=. 3 : s MEAN=. 13 : s mean MEAN õýÃýÃýýýýýýýýýýýýýýÀ õýýýýýÃýÃýÀ þ3þ:þ(+/y.) % (#y.)þ þõýÃýÀþ%þ#þ ÁýÂýÂýýýýýýýýýýýýýýã þþ+þ/þþ þ þ 3 : '(+/y.) % (#y.)' þÁýÂýãþ þ þ ÁýýýýýÂýÂýã +/ % # The explicit form of definition is likely to be more familiar to computer programmers than the tacit form. Translations provided by the adverb 13 : may therefore be helpful in learning tacit programming. An explicit definition of a conjunction may be translated similarly by the adverb 12:. For example: t=. 0 : 0 y.^:_1 @ x.&y. ) under=. 2 : t times=. + under ^. 3 times 4 12