Tom, I agree the step between (f g h) y and f g h y, (where f g h are 3 verbs) can seem a little 'odd' when first encountered.

Taking John and Henry's reply a step further, hooks, forks and tacit definition are all covered in sections in the J Primer (see Help/J Primer or Help/Ndx, then look for these sections).

When no parens are used, an expression is evaluated according to the rules of J (often arguably called right to left). The use of parens ( ) only changes the 'order of execution'.

In the original parent language (APL) such constructs as a string of verbs produced 'Syntax Error' as they were not supported in the language. However J extensions enhanced the language where an expression consists ONLY of a string of 2 or more verbs (verb train) and no parameters (nouns in J). This was desired since what was a syntax error previously could now be utilised to express very powerful mathematical constructs with an even simpler notation (for example average =: +/ % #). The extensions of verb trains go a long way further than this and the power inherent in these constructs is not immediately obvious when you first encounter them, and the alternate notation appears a little confusing.

f g h y is 'usual evaluation of an expression' where the implied order of execution is: f (g (h y)))
however ...
(f g h) y has a first expression inside the parens with no data arguments (nouns), so J first evaluates that expression as a verb train using the parens, as usual, to control order of execution.

A verb train as above is defined in the J Primer as a string of 2 or 3 verbs that is defined as a hook (2 verbs) or fork (3 verbs) (and these trains can nest further):

In the J Primer, I suggest you read the section on Hooks, Forks and Tacit Definition.

Hook: (f g) y is evaluated as y f g y (referred to as the monadic case as there is only one noun argument y) x (f g) y is evaluated as x f g y (referred to as the dyadic case, as there is a left and right noun argument, x and y)

Fork: (f g h) y is evaluated as (f y) g (h y) (monadic case) x (f g h) y is evaluated as (x f y) g (x h y) (dyadic case)

The best way is to experiment using these constructs and compare with/ without the parentheses.
Here is a starter, try to experiment further:

+ % 3 NB. This evaluates as + reciprocal 3, which is just reciprocal 3
0.333333333333
(+ %) 3 NB. Evaluates as 3 + % 3 using hook definition above
3.33333333333
   - % 3
_0.333333333333
   (- %) 3
2.66666666667
2 * - + 3 NB. Think of as 2 * negative 3 (+3 is just 3)
_6
2 (* - +) 3 NB. Evaluates as (2*3) - (2+3) using fork definition above.
1

Hope this helps to get you pointed in the right direction. If still confusing please ask.../Rob Hodgkinson

On 15/11/2008, at 10:32 AM, List wrote:

Clearly that gives the right answer but does not answer the underlying question. I understand that this is relatively elementary, but getting a better handle on the execution difference between 1: + 2: * i.5 and (1: + 2: * i.)5 will help those new to the concepts understand what the heck is going on here.

Tom


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to