functionj

Writing custom verbs in J


When you write your custom verbs(functions) should you use the following convention:

convert degrees fahrenheit to celsius.

   centigrade =: 3 : 0
t1 =. y - 32
t2 =. t1 * 5
t3 =. t2 % 9
)

Alternatively, I have seen this:

ftoc=:(5 % 9)* (-&*32)

Is it necessary to use 3 : 0 and y in first example. What general guidelines should one follow?


Solution

  • The multi-line definition is from the Primer and is intended to show how multi-line explicit definitions are defined and how they work with the debugger. It is NOT an example of good programming practice.

    With a bit of experience you would write:

       f=: 3 : '9%~5*y-32'
       f _40 32 212
    _40 0 100
    

    This could also be done tacitly (without explicit use of y).

       g=: 13 : '9%~5*y-32' NB. get tacit from explicit 
       g
    9 %~ 5 * 32 -~ ]
       g _40 32 100
    _40 0 37.7778