Basic way of adding lists

From J Wiki
Jump to navigation Jump to search


<=   =>

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.

   0 { 7 9 2 4	NB. index 0 value 
7
   1 { 7 9 2 4	NB. index 1 
9
   2 { 7 9 2 4	NB. index 2 
2
   3 { 7 9 2 4	NB. index 3
4


You need to be able to append a new result value to the result list. The verb , can do this.

   7 9 2 , 4	NB. append 4 to the list
7 9 2 4
   7 9 2 4 , 7	NB. append 7 to the list
7 9 2 4 7
   a =. 7 9 2 4
   a =. a , 23
   a
7 9 2 4 23


You need to know how many numbers there are in the list so that you will know when you are done. The monad # (tally) tells us how many numbers are in the list.

   # 7 9 2 4
4
   # 7 9 2 4 7
5


You also need a way to create an empty result to which you will add each new result. An empty string will do this.

   r =. '' 	  NB. an empty string


With these new verbs, combined with what you already know, you can write a Basic or Java style program that adds two lists.

Create a temporary script file and add the addlists definition.

addlists =: dyad : 0
r =. ''
count =. # x
i =. 0
while. i < count do.
 left =. i { x
 right =. i { y
 sum =. left + right
 r =. r , sum
 i =. i + 1
end.
r
)


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 or Mac 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 or Mac: 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 you 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.

adda =: dyad : 0
r =. ''
count =. # x
i =. 0
while. i < count do.
 r =. r , (i { x) + (i { y)
 i =. i + 1
end.
r
)


Run the script and test this new version.

   2 3 4 adda 4 5 6
6 8 10


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.

<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106