Help / Phrases / 2. E. Explicit Definitions
>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases
2E. Explicit Definitions
Explicit definition is convenient for anyone more familiar with conventional programming, particularly that using if-then-else forms. It is also convenient for anyone engaged in extending their mastery of tacit programming: the automatic translation of a one-line explicit definition to tacit form can provide instruction in the reading and writing of tacit expressions. For example:
log=: 3 : '10 ^. y' log 1 10 20 40 100 0 1 1.30103 1.60206 2 log NB. Display of the definition of log 3 : '10 ^. y' log=: 13 : '10 ^. y' log 1 10 20 40 100 0 1 1.30103 1.60206 2 log NB. Display of the (tacit) definition of log 10"_ ^. ]
a0=: def=: : 0 | Adverb for entering explicit definitions |
The phrases 1 : 0 and 2 : 0 and 3 : 0 may be used for entering explicit definitions of adverbs, conjunctions, and functions, without entering enclosing quotes. The adverb def defined above makes their use somewhat more convenient. For example:
LOG=: 3 def 10 ^. y : x ^. y ) LOG 10 1 2 LOG 32 5 rat=: 2 def x&p. % y&p. ) 1 4 6 4 1 rat 1 2 1 1 4 6 4 1&p. % 1 2 1&p. (1 4 6 4 1 rat 1 2 1) i. 6 1 4 9 16 25 36
It is important to recognize that explicit definitions are ordinary J statements (using the conjunction :) , and can be used with other expressions. For example:
mat=: [;._2 (0 : 0) NB. Define matrix one two three ) boxed=: <;._2 (0 : 0) NB. Define boxed list one two three ) fn=: 3 : 0"1 NB. Assign rank to an explicitly defined function < +/ y ) x=: 3 3 : 0 x NB. Execute unnamed explicit definition if. 2|y do. 'odd' else. 'even' end. ) odd mat one two three $mat 3 5 boxed +-------------+ |one|two|three| +-------------+ fn i. 3 4 +-------+ |6|22|38| +-------+