Adding lists

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Primer

# 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.

       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, and the statusbar indicates running and you won't get any result displayed. 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, press Ctrl+Break to interrupt the execution.

In fact, it is worthwhile seeing how this looks. 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. You should see that the statusbar indicates running and that there is no result. Press Ctrl+Break to stop the execution.

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.

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Primer