>>  <<  Usr  Pri  JfC  LJ  Phr  Dic  Voc  !:  Help  Dictionary

18. Explicit Definition

The sentences in the example:
   m=: '3 %: y'
   d=: 'x %: y'
that were executed in the discussion of word formation, can be used with the explicit definition conjunction : 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 Section 3
)
  
   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              
if. y<0            
  do. *:y             
  else. %:y           
end.            
)
                 
   f"0 (_4 4)    
16 2                 
factorial=: 3 : 0
a=. 1
while. y>1
do. a=. a*y
y=. y-1 end. a
)

   factorial"0 i. 6
1 1 2 6 24 120

Exercises

18.1   Experiment with and display the functions roots=: 3 : script and 13 : script (which are equivalent).

18.2   See the discussion of control structures in the dictionary, and use them in defining further verbs.

18.3   Experiment with expressions such as ! d b=: i.7 , after defining the adverb d :
d=: 1 : 0
+:@x
)
18.4   Using the program pol from Exercise 14.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).

18.5   Review Section H of Part II and use scripts to make further explicit definitions.

18.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=: 1 : 0
-:@(x f. + x f.@-)
)
ge=: g even
(e = ge) b
(e = e even) b
18.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
18.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



>>  <<  Usr  Pri  JfC  LJ  Phr  Dic  Voc  !:  Help  Dictionary