2 %: b Square root of b 2 2.23607 2.44949 2.64575 2.82843 3 3 %: b Cube root of b 1.5874 1.70998 1.81712 1.91293 2 2.08008 (+/ % #) b Arithmetic mean or average 6.5 (# %: */) b Geometric mean 6.26521 (] - (+/ % #)) b Centre on mean (two forks) _2.5 _1.5 _0.5 0.5 1.5 2.5 (] - +/ % #) b Two forks (fewer parentheses) _2.5 _1.5 _0.5 0.5 1.5 2.5 a (+ * -) b Dyadic case of fork 48 24 0 _24 _48 _72 (a^2)-(b^2) 48 24 0 _24 _48 _72 a (< +. =) b Less than or equal 0 0 1 1 1 1 a./) b=. 2 7 1 8 2 ANSWER: #b repetitions of the maximum over b 6.2 Cover the comments on the right, write your own interpretation of each sentence, and then compare your statements with those on the right: (+/ % #) b Mean of b (# # +/ % #) b (n=.#b) repetitions of mean +/(##+/%#) b Sum of n means (+/b)=+/(##+/%#) b Tautology (*/b)= */(###%:*/) b The product over b is the product over n repetitions of the geometric mean of b. 7: PROGRAMS A program handed out at a musical evening describes the sequence of musical pieces to be performed. As suggested by its roots gram and pro, a program is something written in advance of the events it prescribes. Similarly, the fork +/ % # of the preceding lesson is a program that prescribes the computation of the mean of its argument when it is applied, as in the sentence (+/%#)b. However, we would not normally call the procedure a program until we assign a name to it, as illustrated below: mean=. +/ % # mean 2 3 4 5 6 4 (geomean=. # %: */) 2 3 4 5 6 3.72792 Since the program mean is a new verb, we also refer to a sentence such as mean=. +/ % # as verb definition (or definition), and to the resulting verb as a definedverb or function. Defined verbs can be used in the definition of further verbs in a manner sometimes referred to as structured programming. For example: MEAN=. sum % # sum=. +/ MEAN 2 3 4 5 6 4 Entry of a verb alone (without an argument) displays its definition, and the foreign conjunction can be used to specify the form of the display: boxed, tree, or linear: mean +/ % # 9!:3 (2 4 5) mean õýýýýýÃýÃýÀ þõýÃýÀþ%þ#þ þþ+þ/þþ þ þ þÁýÂýãþ þ þ ÁýýýýýÂýÂýã õý / ýýý + ýýÊý % Áý # +/ % # Exercises: 7.1 Enter AT=. i. +/ i. and use expressions such as AT 5 to determine the behaviour of the program AT. 7.2 Define and use similar function tables for other dyadic functions. 7.3 Define the programs: tab=. +/ ft=. i. tab i. test1=. ft = AT Then apply test1 to various integer arguments to test the proposition that ft is equivalent to AT of Exercise 7.1, and enter ft and AT alone to display their definitions. 7.4 Define aft=. ft f. and use test2=. aft = ft to test their equivalence. Then display their definitions and state the effect of the adverb f. ANSWER: The adverb f. fixes the verb to which it applies, replacing each name used by its definition. 7.5 Redefine tab of Exercise 7.3 by entering tab=. */ and observe the effects on ft and its fixed alternative aft. 7.6 Define mean=. +/ % # and state its behaviour when applied to a table, as in mean t=. (i. !/ i.) 5. ANSWER: The result is the average of, not over, the rows of a table argument. 7.7 Write an expression for the mean of the columnsáof t. ANSWER: mean |: t or mean"1 t 8: BOND CONJUNCTION A dyad such as ^ can be used to provide a family of monadic functions: ]b=. i.7 0 1 2 3 4 5 6 b^2 Squares 0 1 4 9 16 25 36 b^3 Cubes 0 1 8 27 64 125 216 b^0.5 Square roots 0 1 1.41421 1.73205 2 2.23607 2.44949 The bond conjunction & can be used to bind an argument to a dyad in order to produce a corresponding defined verb. For example: square=. ^&2 Square (power and 2) square b 0 1 4 9 16 25 36 (sqrt=. ^&0.5) b Square root function 0 1 1.41421 1.73205 2 2.23607 2.44949 A left argument can be similarly bound: Log=. 10&^. Base-10 Logarithm Log 2 4 6 8 10 100 1000 0.30103 0.60206 0.778151 0.90309 1 2 3 Such defined verbs can of course be used in forks. For example: in29=. 2&< *. <&9 Interval test in29 0 1 2 5 8 13 21 0 0 0 1 1 0 0 IN29=. in29 # ] Interval selection IN29 0 1 2 5 8 13 21 5 8 LOE=. <+.= 5 LOE 3 4 5 6 7 0 0 1 1 1 integertest=. <. = ] The monad <. is integer part or floor integertest 0 0.5 1 1.5 2 2.5 3 1 0 1 0 1 0 1 int=. integertest int (i.13)%3 1 0 0 1 0 0 1 0 0 1 0 0 1 Exercises: 8.1 The verb # is used dyadically in the program IN29. Enter expressions such as (j=. 3 0 4 0 1) # i.5 to determine the behaviour of #, and state the result of #j#i.5. (Also try 1j1#i.5.) ANSWER: +/j 8.2 Cover the answers on the right and apply the following programs to lists to determine (and state in English) the purpose of each: test1=. >&10 *. <&100 Test if in 10 to 100 int=. ] = <. Test if integer test2=. int *. test1 Test if integer and in 10 to 100 test3=. int +. test1 Test if integer or in 10 to 100 sel=. test2 # ] Select integers in 10 to 100 8.3 Cover the program definitions on the left of the preceding exercise, and make new programs for the stated effects. 8.4 Review the use of the fix adverb in Exercises 7.4-5, and experiment with its use on the programs of Exercise 8.2. 9: ATOP CONJUNCTION The conjunction @ applies to two verbs to produce a verb that is equivalent to applying the first atop the second. For example: TriplePowersOf2=. (3&*)@(2&^) TriplePowersOf2 0 1 2 3 4 3 6 12 24 48 CubeOfDiff=. (^&3)@- 3 4 5 6 CubeOfDiff 6 5 4 3 _27 _1 1 27 f=. ^@- The rightmost function is first applied monadically; the second is applied dyadically if possible. 5 f 3 7.38906 f 3 0.0497871 g=. -@^ 5 g 3 _125 g 3 _20.0855 A conjunction, like an adverb, is executed before verbs; the leftargument of either is the entire verb phrase that precedes it. Consequently, some (but not all) of the parentheses in the foregoing definitions can be omitted. For example: COD=. ^&3@- 3 4 5 6 COD 6 5 4 3 _27 _1 1 27 TPO2=. 3&*@(2&^) TPO2 0 1 2 3 4 3 6 12 24 48 tpo2=. 3&*@2&^ An error because the conjunction @ is defined |domain error only for a verb right argument | tpo2=. 3&*@2&^ Exercises: 9.1 Cover the comments on the right, and state the effects of the programs. Then cover the programs and rewrite them from the English statements: mc=. (+/%#)@|: Means of columns of table f=. +/@*: Sum of squares of list g=. %:@f Geometric length of list h=.{&' *'@(:&2 Interval test 2 to 9 (inclusive) f4=. f3 *. <. = ] In 2 to 9 and integer f5=. f3 +. <. = ] In 2 to 9 or integer g1=. %&1.8 Divide by 1.8 g2=. g1^:_1 Multiply by 1.8 g3=. -&32 Subtract 32 g4=. g3^:_1 Add 32 g5=. g1@g3 Celsius from Fahrenheit g6=. g5^:_1 Fahrenheit from Celsius h1=. >./ Maximum over list (monad) h2=. h1-<./ Spread. Try h2 b with the parabola: b=.(-&2 * -&3) -:i.12 h3=. h1@]-i.@[*h2@]%<:@[ Grid. Try 10 h3 b h4=. h3 <:/ ] Barchart. Try 10 h4 b h5=. {&' *' @ h4 Barchart. Try 10 h5 b After entering the foregoing definitions, enter each verb name alone to display its definition, and learn to interpret the resulting displays. To see several forms of display, first enter 9!:3 (2 4 5). Cover the left side of the page, and translate the English definitions on the right back into J. Exercises: 13.1 These exercises are grouped by topic and organized like the lesson, with programs that are first to be read and then to be rewritten. However, a reader already familiar with a given topic might begin by writing. A. Properties of numbers pn=. >:@i. Positive numbers (e.g. pn 9) rt=. pn |/ pn Remainder table dt=. 0&=@rt Divisibility table nd=. +/@dt Number of divisors prt=. 2&=@nd Prime test prsel=. prt # pn Prime select N=. >:@pn Numbers greater than 1