home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / J_OS2.ZIP / WHAT_IS.J < prev   
Text File  |  1992-03-16  |  14KB  |  424 lines

  1.  
  2.                What is J?
  3.  
  4.             An introduction
  5.  
  6.                 Part 1
  7.  
  8.                  by
  9.  
  10.             Leroy J. Dickey
  11.  
  12.             October, 1991
  13.  
  14.  
  15. J is a high powered general purpose programming language.  This dialect
  16. of APL uses the ASCII character set, has boxed arrays, complex numbers,
  17. and phrasal forms.  Any time one wants to do calculations with more
  18. than one number at a time, or more than one name in a list, a
  19. collective (an array) is the right structure to use, and J is designed
  20. to make it easy to do collective calculations.
  21.  
  22. People who write programs for themselves and who want their answers
  23. quickly will be happy with the swift and concise way programs can be
  24. written in J.
  25.  
  26. This article consists of several examples that illustrate some of the
  27. power of the language J.  In each of the examples presented, output
  28. from an interactive J session has been captured, and few lines of
  29. explanation have been added.  Lines that are indented with four spaces
  30. are those that were typed in to the J interpreter, and the lines that
  31. are indented with only one space are the responses from J.  Other text
  32. lines are comments that have been added later.  The topics that have
  33. been chosen for inclusion do not come close to telling everything about
  34. J, but some of them represent subjects that at one time or another the
  35. author wished that he had known and understood.
  36.  
  37. Because my professional interests are mathematical, my examples are
  38. primarily mathematical in nature.  But J is a general purpose computing
  39. language, and is well suited to a broad spectrum of programming needs.
  40.  
  41. In this discussion about J, the words noun and verb are used to stand
  42. for data and function, respectively.  Thus 'abc' is a noun,  1 2 3 4
  43. is a noun, and + is a verb.  One may assign data to a name, as in the
  44. statement    alpha5=.'abcde', and the name, (such as "alpha5"), used to
  45. refer the data, is said to be a pronoun.  Similarly, one may associate
  46. a name with a verb, and such a name is said to be a proverb,
  47. (pronounced "pro-verb", not "prah-verb").
  48.  
  49. Thus, in the assignment
  50.     plus =. "+"
  51. the name "plus" is a proverb used to refer to the verb "+".  That is,
  52. "plus" stands for "+".  There are other language elements called
  53. conjunctions, which join two verbs or proverbs, and there are elements
  54. called adverbs, which modify the meaning one verb or proverb.
  55.  
  56.  
  57. Example 1:  Functional notation
  58.  
  59. This example shows how calculations are done with a list of numbers and
  60. how the notation is functional.  That is, each verb (function) acts on
  61. the data to its right.   It is easy to create a collective.  Here, for
  62. instance, are 9 numbers:
  63.  
  64.     i. 9
  65.  0 1 2 3 4 5 6 7 8
  66.  
  67.     a =. i. 9
  68.     a
  69.  0 1 2 3 4 5 6 7 8
  70.  
  71. The factorials of these numbers are:
  72.     ! a
  73.  1 1 2 6 24 120 720 5040 40320
  74.  
  75. The reciprocals of the above:
  76.     % ! a
  77.  1 1 0.5 0.166667 0.0416667 0.00833333 0.00138889 0.000198413 2.48016e_5
  78.  
  79. And the sum of reciprocals of the factorials of a is:
  80.     +/ % ! a
  81.  2.71828
  82.  
  83.  
  84. Those who know some mathematics may recognize an approximation to the
  85. number ``e'', the base for the natural logarithms.  Of course J has
  86. other ways to calculate this number; the point here is the use of the
  87. natural left to right reading of the meaning of the sequence symbols
  88. "+/%!a" as "the sum of the reciprocals of the factorials of a".
  89.  
  90. Those who have done some programming in almost any other language, will
  91. see that the expression "+/%!i.9" is a remarkably short one to produce
  92. an approximation to ``e''.  Moreover, it is possible to pronounce this
  93. program in English in a way that precisely conveys its meaning, and
  94. those who know the meaning of the words in the sentence will be able to
  95. understand it.  And anybody who knows the mathematics and who knows J
  96. will recognize that the program does exactly what the words say and
  97. that it will produce an approximation to ``e''.
  98.  
  99. The author finds this expression in J much easier to think about and
  100. to understand than a corresponding program in a ``traditional'' language
  101. intended to compute the same number.  Here, for example, is what one
  102. might write in Fortran, to accomplish the same result:
  103.  
  104.  
  105.     REAL SUM, FACTRL
  106.     SUM = 1.0
  107.     FACTRL = 1.0
  108.     DO 10 N = 1, 8
  109.         FACTRL = FACTRL * N
  110.         SUM = SUM + 1.0 / FACTRL
  111.      10 CONTINUE
  112.     PRINT, SUM
  113.     STOP
  114.     END
  115.  
  116.  
  117. Compare this Fortran program with the J program that uses only a few
  118. key strokes: +/ % ! i. 9 .  Not only is the J program shorter, but it
  119. is easier to understand, even for the person who has just learned the
  120. meaning of the symbols.  Some have estimated that for typical
  121. applications, the ratio of the number of lines of Fortran or C code to
  122. the number of lines of J code is about 20 to 1.
  123.  
  124.  
  125.  
  126. Example 2:
  127.  
  128. In this example, two verbs (functions) are defined.  As before, lines
  129. indented with four spaces were given as input during a J session, and
  130. the ones immediately after, with one leading space, were produced by
  131. the J interpreter.  Other lines are comments, and were added later.
  132.  
  133.  
  134.     Sum =. +/
  135.     Average =. Sum % #
  136.     c =. 1 2 3 4
  137.     Average c
  138.  2.5
  139.  
  140. The first two lines create proverbs, the third creates a pro-noun,
  141. and the fourth invokes the proverb (function) Average with the
  142. pronoun (data) c.  The meaning of   Average c  is this:
  143.  
  144.         (+/ % #) 1 2 3 4
  145.  
  146. and this may be thought of as:  find Sum c, (add up the entries
  147. in c), find # c (the count of c), and then find the quotient of those
  148. two results.
  149.  
  150. Example 3:  Continued fractions
  151.  
  152. In this example, the verb ``pr'' (plus reciprocal), is considered.
  153.  
  154.     pr =. +%
  155.  
  156. The next two input lines show a use of this hook.  From them you
  157. can see that the meaning of "a pr b" is "a plus the reciprocal of b":
  158.  
  159.     5 pr 4
  160.  5.25
  161.  
  162.     4 pr 5
  163.  4.2
  164.  
  165. The function "pr" is defined above as  +% , and this diagram 
  166. for    4 +% 5   may help you to understand its meaning.
  167.  
  168.                          +
  169.                         /  \
  170.                        4    %
  171.                             |
  172.                             5
  173.  
  174. Because of the shape of the diagram, the combination of two verbs
  175. in this way is called a hook.  To continue with our example,
  176.  
  177.     1 pr 1 pr 1            
  178.  1.5
  179.  
  180. The above input line may be read as ``1 plus the reciprocal
  181. of (1 plus the reciprocal of 1)''.  And here below is an analogous
  182. line with four ones:
  183.  
  184.     1 pr 1 pr 1 pr 1
  185.  1.66667
  186.  
  187.     1 pr (1 pr (1 pr 1))
  188.  1.66667
  189.  
  190.     pr / 1 1 1 1        
  191.  1.66667
  192.  
  193.     pr / 4 # 1
  194.  1.66667
  195.  
  196. The above input line has exactly the same meaning as the three input
  197. lines that come before it.  Notice that (of course) the value
  198. of the result is the same.  The "/" is an adverb called "over"
  199. and the result of ``pr /'' is a new verb, whose meaning is
  200. derived from the verb ``pr''.
  201.  
  202. This expression could be written another way, using a traditional
  203. mathematical notation the way that mathematicians have been using
  204. to write continued fractions for years:
  205.  
  206.         1
  207.    1 + ------------------------
  208.         1
  209.     1 + ------------------
  210.             1
  211.         1 + ------------
  212.             1
  213.  
  214. This particular continued fraction is a famous one because it is
  215. known to converge to the golden ratio.  That means that if you
  216. just keep on going, with more and more ones, the value gets closer and
  217. closer to the golden ratio.  One can see the values of the continued
  218. fraction expansion "converging" by using "\", the scan adverb.  It
  219. creates a sequence of initial sequences.  Here is a nice, compact
  220. expression that shows the values for the first 15 partial continued
  221. fractions.
  222.  
  223.     pr /\ 15$ 1
  224.  1 2 1.5 1.66667 1.6 1.625 1.61538 1.61905 1.61765 1.61818 1.61798
  225.       1.61806 1.61803 1.61804 1.61803
  226.  
  227. In this, you can see that the numbers seem to be getting closer and
  228. closer to some limit, probably between 1.61803 and 1.61804.  If you
  229. concentrate only on the odd numbered terms, (the first, the third, the
  230. fifth, and so on), you will see a sequence of numbers that is
  231. monotonically increasing.  On the other hand, the even numbered terms,
  232. ( 2, 1.66667, 1.625, and so on), form a sequence of numbers that is
  233. monotonicly decreasing.  Of course these observations do not constitute
  234. a proof of convergence, but they might convince you that a proof can
  235. be found, and in the light of these observations, the proof probably
  236. follows along these lines.
  237.  
  238. Can you think of a simpler way to represent and evaluate continued
  239. fractions?  I can not!
  240.  
  241. The golden ratio has been known for thousands of years, and is the
  242. number, bigger than one, such that itself minus 1 is its own reciprocal.
  243. The golden ratio is usually denoted by the lower case greek letter tau.
  244. It is said that pictures whose sides are in the ratio 1 to tau are
  245. the most pleasing to the eye.
  246.  
  247. Example 4.  Using rank operator and fork.
  248.  
  249. In this example, a second phrasal form, the fork, is discussed.
  250. First some data are built.
  251.  
  252.     a =. 100 200
  253.     b =. 10 20 30
  254.     c =. 1 2 3 4
  255.  
  256. This is the beginning of the data construction.  We use the verb
  257. Sum, a modification of the verb plus, and it is defined as before:
  258.     Sum =. +/
  259. But in this example, the usage is different. To get an idea about
  260. the dyadic action of the verb Sum, notice what Sum does with
  261. just b and c:
  262.  
  263.     b Sum c
  264.  11 12 13 14
  265.  21 22 23 24
  266.  31 32 33 34
  267.  
  268. Once you have seen this example, you are ready to see and understand
  269. the next step.  Here the collective ``data'' is built, using a, b,
  270. and c.
  271.  
  272.     data =. a Sum b Sum c
  273.     data
  274.  111 112 113 114
  275.  121 122 123 124
  276.  131 132 133 134
  277.  
  278.  211 212 213 214
  279.  221 222 223 224
  280.  231 232 233 234
  281.  
  282. This rank three array may be thought of as having has two planes,
  283. each having three rows and four columns.
  284.  
  285.     $ data
  286.  2 3 4
  287.  
  288. Now examine what "Sum" does with the data:
  289.  
  290.     Sum data
  291.  322 324 326 328
  292.  342 344 346 348
  293.  362 364 366 368
  294.  
  295. Can you see what is happening?  It is adding up corresponding
  296. elements in the two planes.  Twelve different sums are performed,
  297. and the result is an array that is 3 by 4.  The action happens
  298. over the first dimension of the array.
  299.  
  300. But we might wish to add up numbers in rows.  We can do it this way:
  301.  
  302.     Sum "1 data
  303.  450 490 530
  304.  850 890 930
  305.  
  306. And to add up numbers in each column:
  307.  
  308.     Sum "2 data
  309.  363 366 369 372
  310.  663 666 669 672
  311.  
  312. The expressions "1 and "2 are read as 'rank 1' and 'rank 2'.  The rank
  313. 1 objects of 'data' are the rows, the rank 2 objects are the planes,
  314. and the rank 3 object is the whole object itself.  So, when one asks
  315. for  Sum "2 data ,  one asks for the action to happen over the first
  316. dimension of the rank two arrays.
  317.  
  318. Now, recall the definition of Average.
  319.  
  320.     Average =. Sum % #
  321.  
  322. Apply this proverb "Average" to the pronoun "data", to get:
  323.  
  324.     Average data
  325.  161 162 163 164
  326.  171 172 173 174
  327.  181 182 183 184
  328.  
  329. The result is the average of corresponding numbers in the two planes.
  330. That is, the average is calculated over the objects along the first
  331. dimension of data.  If we would like to know the average of the
  332. numbers in the rows, we type:
  333.  
  334.     Average "1 data
  335.  112.5 122.5 132.5
  336.  212.5 222.5 232.5
  337.  
  338. and finally, averaging the columns of the two planes:
  339.  
  340.     Average "2 data
  341.  121 122 123 124
  342.  221 222 223 224
  343.  
  344.  
  345. Again, compare the sizes of these results with the sizes of
  346. the sizes of the results above where we were asking simply about
  347. sums.
  348.  
  349.     Sum "1 data
  350.  450 490 530
  351.  850 890 930
  352.  
  353.     Sum "2 data
  354.  363 366 369 372
  355.  663 666 669 672
  356.  
  357. What is exciting about this example is not only how easily the nouns
  358. and verbs were built, but how every verb acts in a uniform and
  359. predictable way on the data.  Rank one action is the same kind of
  360. action whether one is summing, or averaging, or whatever.  It has been
  361. said that the concept of rank is one of the top ten ideas to come along
  362. in computing in the last decade.
  363.  
  364.  
  365. Conclusion:
  366.  
  367. What has been written in this article only begins to scratch the
  368. surface of the power of J, but it does show some of the flavor.  J is
  369. powerful because one get results quickly.  It is a programming language
  370. for people who need to write programs for themselves, but who don't
  371. want to spend inordinately large amounts of time getting their
  372. results.  If you have to write programs for others, this could still be
  373. a language for you, if you get paid by the job, and not by time.
  374.  
  375.  
  376. Availability:
  377.  
  378. J is available by "ftp" from watserv1.waterloo.edu in the directory
  379. "languages/apl/j" and is also available from certain other servers.
  380. At the Waterloo site, versions of J are available that run on several
  381. kinds of computers.
  382.  
  383.  
  384. Learning J:
  385.  
  386. Because J is new, there are not many materials about J yet.
  387. The novice should get a copy of J and let J be the guide and
  388. the interpreter (pun intended).  There is a tutorial that has
  389. tons of material in a small space.  This tutorial comes with
  390. the interpreter.  There is also a "status.doc" file that comes
  391. with J.  This tell what features have been implemented and
  392. which have not.
  393.  
  394.  
  395. The author of J:
  396.  
  397. The inventor-developer of J is Kenneth E. Iverson, the same man who
  398. invented APL, a real pioneer of computing.  Some years ago, he retired
  399. from IBM and went to work for I.P.Sharp Associates, a Toronto company
  400. that developed the world's first private packet switching network,
  401. the finest APL available, and was at one time the worlds largest
  402. time sharing service, all APL.  The descendant of this company,
  403. Reuter:file, still has offices in many of the world's major cities.
  404. Now Iverson is retired (again) and lives in Toronto, where he spends
  405. his time developing and promoting J.
  406.  
  407. The programmer for J is Roger Hui.  Roger hails from Edmonton, and has
  408. lived in Toronto since he joined I.P.Sharp Associates in 1975.  The
  409. source code for J, written in C, is worthy of some comment, because it
  410. makes extensive use of macro expansion.  Its style, influenced by the
  411. work of Arthur Whitney of Morgan Stanley in New York, reflects Roger's
  412. strong background in APL.  Today, Roger spends much of his time
  413. developing J and does occasional contract work in APL.
  414.  
  415.  
  416. Copyright (c) 1991 by Leroy J. Dickey
  417. Permission is granted to use this for
  418. non-profit and educational purposes.
  419. It may be given away, but it may not be sold.
  420.  
  421.  
  422. Revised 1991-12-04
  423.  
  424.