Basic way of adding lists

You can have lists of numbers.

a =. 12 24 47
b =. 12 34 45

If you wanted to add two lists of numbers in a language like Basic you would have to get each number in turn from each list, add them together, and then stick this new result at the end of the result list.

To add two lists of numbers together in this way you need a few new primitives. You need a way to get one number from a list. The verb { (from) can do this.

The local i is the index to select numbers from each list. It starts with 0 to select the first number from the left and right arguments. At the end of the while. control structure the i is incremented by 1 so that the next time the block is executed it will select the next number. The while. structure tests to see if i is less than the count of the argument. The control structure is finished when i is equal to the count of numbers to be added together. The left and right locals are defined as the next pair of numbers. They are added together and are appended to the end of the result r. The final line in the definition is r and that is the result.

Run the script and test your definition of addlists.

2 3 4 addlists 4 5 6
6 8 10

If you made a typo in the definition you will get an error or a wrong answer. In that case, you should check carefully that you have typed the definition in correctly.

Certain errors (such as omitting the line that incremented the value of i) give you a while. that runs forever. This is because the while. never ends and the program keeps adding the first element of the left and right arguments and never steps to the next element. If you are in a loop like this, you need to run the jbreak program to interrupt J execution. This is the yellow J icon in Windows and jbreak shell script in Unix.

In fact, it is worthwhile seeing how this looks. First be sure you know how to run the jbreak program to signal the interrupt (in Windows: find the yellow J icon, and in Unix have a command window ready to run the jbreak shell script in the J directory). Running jbreak while J is idle doesn't do anything. Edit the addlists definition so that i is not incremented. The easiest way to do this is to add NB. in front of the i =. i + 1 sentence. Run the script and test the verb. This will run until to use jbreak to interrupt execution (or cancel the J task).

For such a simple thing, this definition seems overly verbose in taking eleven lines. The definition can be compacted a bit by combining sentences. In the temporary script file create a second version of the definition.

This is essentially how programmers in most languages add two lists of numbers. The program could be further streamlined, but it would still have to be a control structure that dealt with each number, one at a time. Most languages only know how to add a single number to a single number, and to add lists of numbers, you need to write a control structure that loops and explicitly adds each element of the list in turn.>><<UsrPriJfCLJPhrDicRelVoc!:Help
Primer