Fifty Shades of J/Chapter 10

From J Wiki
Jump to navigation Jump to search

Table of Contents ... Glossary ... Previous Chapter ... Next Chapter

Bonding is Power – how Interesting

Principal Topics

|. (shift) ^: (power conjunction) currying, compound interest, savings schemes, annuities, net present value.


Warning.png The dyadic forms of m&v and u&n used here have been removed from J Version 9.6 and later

To get the same result, write them as x m&v@]^:[ y and x u&n@]^:[ y

Bonding and Power

“All verbs are monadic” may sound a heretical statement, after all what about 2+3? At the surface level the left and right arguments have equal force, but delve a little more deeply, and at interpretation time in the computer one argument must be fetched before the other, in which sense argument marshalling is not symmetric, that is 2+3 is not the same as 3+2. In the case of 2+3 an intermediate unarticulated verb, either 2&+ or +&3 is created to which the other argument is presented monadically. The power conjunction helps to determine which, i.e. does 2+^:(4)3 mean 2 added 4 times to 3, or 3 added 4 times to 2? The answer is determined by

   ((2&+)^:4)3
11
   ((+&3)^:4)2
14

that is it is the left argument which is more strongly bound. More generally in computer science the idea of argument bonding is known as currying after the American logician H. Curry, and the J documentation explicitly offers ‘curry’ as alternative name for bond (&).

Already the link between bond and the power conjunction is becoming clear, and this is made explicit in the J help file by the formal definition of bond

x m&n y ↔ m&v*:x y

To see how this works with lists look at the following line in which 4 and 40 are added progressively to start values 100 and 1000 several times

   1 2 3(+&4 40)100 1000
104 1040
108 1080
112 1120

The above describes the process of accumulating simple interest at 4%. The difference with compound interest is that it is a process of progressive multiplication

   1 2 3(*&1.04 1.05)100 1000     NB. compound interest
    104    1050
 108.16  1102.5
112.486 1157.63

Replace multiplication with division, and the result is net present values

    1 2 3(%&1.04 1.05)100 1000    NB. net present value
96.1538 952.381
92.4556 907.029
88.8996 863.838

which can also be obtained by using negative power values with multiplication

   _1 _2 _3(*&1.04 1.05)100 1000
96.1538 952.381
92.4556 907.029
88.8996 863.838

Savings schemes

A scheme whereby regular sums are added periodically to a capital sum which attracts compound interest is a mixture of both these processes, and the verb base can be brought into the picture. 100 #.1.05 4 equals 109 and so

   1 2 3 4 5(#.&1.05 4)100
109 118.45 128.373 138.791 149.731

shows the progress of such a scheme. With a little help from the rank conjunction the calculation can be replicated for several initial capitals

    1 2 3 4 5(#.&1.05 4)"(1)0,.100 1000 2000 10000
  109  118.45 128.373 138.791 149.731
 1054  1110.7 1170.24 1232.75 1298.38
 2104  2213.2 2327.86 2448.25 2574.67
10504 11033.2 11588.9 12172.3 12784.9

An annuity is just a savings scheme with a negative addition in each period and thus in general a reducing value

   1 2 3 4 5(#.&1.05 _6)100
99 97.95 96.8475 95.6899 94.4744

2&v and v&2 are different for non-commutative verbs. The difference is most readily seen by comparing the boxes

   (1 2 3 4 5(%&2 3)6);1 2 3 4 5(2 3&%)6
┌────────────────┬────────────┐
│     3         2│0.333333 0.5│
│   1.5  0.666667│       6   6│
│  0.75  0.222222│0.333333 0.5│
│ 0.375 0.0740741│       6   6│
│0.1875 0.0246914│0.333333 0.5│
└────────────────┴────────────┘

In the left hand box, 2 and 3 are progressively divided by 6; in the right hand box 6 is progressively divided by 2 and 3, and dividing by the previous quotient is a null operation.

Progressive series

The straightforward evaluation of a series such as 3n+2 would routinely be carried out by say

   (3*i.10)+2
2 5 8 11 14 17 20 23 26 29

“Progressive evaluation” means feeding back each evaluation as the next n. The result of the first five values of such a procedure for 3n+2 is

   1 2 3 4 ((3&+)&2)1
5 17 53 161

the last of which values could more mundanely be obtained as

   (3*(3*(3*((3*1)+2))+2)+2)+2
161

The simple evaluation of the series 3n+2 is obtained with a left argument of 1

   1 ((3&+)&2)i.10
2 5 8 11 14 17 20 23 26 29

Adding &(^&2) squares the left argument

   2 (((3&+)&2)&(^&2)) 1
161

and adding square to the left gives progressive values of (3n+2)2

   2 3 ((*:@(3&+)&2)) 0
196 348100

which are the squares of 14 and 590 respectively.

Non-numeric applications

Progessive left shifts of a list

   0 1 2(1&|.)'conundrum'
conundrum
onundrumc
nundrumco
   0 1 2(3&|.)'conundrum'
conundrum
undrumcon
rumconund

an effect which could also be achieved using the rank conjunction

   0 3 6 |."0 1 'conundrum'
conundrum
undrumcon
rumconund

Progressive reduction of a list

   0 1 2(2&}.)'conundrum'
conundrum
nundrum
ndrum

Progressive repetition of a list

   1 2 3 ('abc'&,)''
abc
abcabc
abcabcabc

as opposed to repetitions of every item separately

   1 2 3 #every <'abc'
abc
aabbcc
aaabbbccc

Together the bond conjunction and the power conjunction are enormously versatile weapons in the J armoury.

Script

File:Fsojc10.ijs