Understanding Add from List in J

I am working on a book on J and get answers to the exercises, but it’s hard for me to understand why they work.

Here is the question:

Peter and Pauls golf glasses give a twist in the form of a β€œy.” List, such as 3 4 4 4 5 6 3 3 7 2, etc. Write an expression that gives the difference between the total points of Peters and the total points of Pauls. Write another expression that will get the general result for Peter and Paul at 18 holes.

The answer to the first question:

-/y

I understand that this is the insertion of n-1 subtraction characters between each element in the list, effectively getting the difference between their ratings in each hole and giving you the sum at the end.

The answer to the second question:

(36$0 1)+//.y

I do not understand how to think through this answer. From right to left,

  • You take the list "y"
  • insert n-1 copies of the verb into this list
  • "+/" , ...
  • ...??? .

, , . , /.

, , , .:)

+4
1

/.:

x v/. y v y, x:

0 0 1 0 1 2 < /. 1 2 3 4 5 6
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”
β”‚1 2 4β”‚3 5β”‚6β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”˜

0 0 1 0 1 2 +/ /. 1 2 3 4 5 6
7 8 6

(36 $ 0 1), , , +/ (, y =: i.36):

36 $ 0 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
(36 $ 0 1) < /. y
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34β”‚1 3 5 7 9 11 13 15 17 19 21  23 25 27 29 31 33 35β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
(36 $ 0 1) +//. y
306 324

For this particular task, I would prefer it to fit the shape $ yand simply summarize the corresponding axis. For instance:

18 2 $ y     NB. or even better _2 ,\y
0  1
2  3
4  5
...
+/ 18 2 $ y
306 324
+5
source

Source: https://habr.com/ru/post/1692999/


All Articles