Studio/OrderOfExecution

From J Wiki
Jump to navigation Jump to search

Unlike most other programming languages, there is no precedence between verbs, or between adverbs and conjunctions. Like other languages, parentheses can be used to specify the sequence of execution.

You can often solve problems with precedence by adding parentheses to an expression:

Equivalent to 9-(3-4):

   9-3-4
10

Parentheses change order of execution:

   (9-3)-4
2

Takes sum of the beheaded list:

    +/ }. 1 2 3
5

Applies the hook +/ }. to 1 2 3 :

   (+/ }.) 1 2 3
3 4
4 5
5 6

    a=. +/ }.
    a 1 2 3
3 4
4 5
5 6

Boxed display is useful for showing the order of execution:

Shows that the rightmost adverb applies to the derived verb +/ . * and that the left argument of the dot product is the derived verb +/

   +/ . * /
+-----------+-+
|+-----+-+-+|/|
||+-+-+|.|*|| |
|||+|/|| | || |
||+-+-+| | || |
|+-----+-+-+| |
+-----------+-+