home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / j / dos / what_is.j < prev    next >
Text File  |  1992-03-24  |  18KB  |  505 lines

  1.  
  2.                           What is J?
  3.  
  4.             An introduction
  5.  
  6.                  by
  7.  
  8.             Leroy J. Dickey
  9.              University of Waterloo
  10.  
  11.  
  12. This article is intended to be a first introduction to the language J
  13. and some of its features.  J is a high powered general purpose
  14. programming language.  This dialect of APL uses the ASCII character
  15. set, has boxed arrays, complex numbers, the rank operator, and some
  16. novel compositions of functions.
  17.  
  18. Like APL, J is an array language.  Any time one wants to do
  19. calculations with more than one number at a time, or with more than one
  20. name in a list, a collective (an array) is the right structure to use,
  21. and J is designed to do such calculations easily.
  22.  
  23. Those who write programs for themselves and who want their answers
  24. quickly will be happy with the swift and concise way programs can be
  25. written in J.
  26.  
  27. This article consists of several examples that illustrate some of the
  28. power of the language J.  Each example presents input and output from
  29. an interactive J session, and a few lines of explanation have been
  30. added.  Lines that are indented with four spaces are those that were
  31. typed in to the J interpreter, and the lines that are indented with
  32. only one space are the responses from J.  Other text lines are comments
  33. that have been added later.  The topics that have been chosen for
  34. inclusion do not come close to telling everything about J, but some of
  35. them represent subjects that at one time or another the author found
  36. new and exciting.
  37.  
  38. J is a general purpose computing language, and is well suited to a
  39. broad spectrum of programming needs.  Because the interests of the
  40. author are in mathematics, examples are primarily mathematical in
  41. nature.
  42.  
  43. In this discussion about J, the words noun and verb are used to stand
  44. for data and function, respectively.  Thus 'neon' is a noun,  1 2 3 4
  45. is a noun, and + is a verb.  One may assign data to a name, as in the
  46. statement    alpha5=.'neon' , and a name (such as alpha5) used to refer
  47. to the data is said to be a pronoun.  Similarly, one may associate a
  48. name with a verb, and such a name is said to be a proverb,
  49. (pronounced "pro-verb", not "prah-verb").
  50.  
  51. In the assignment
  52.     plus =. +
  53. the name plus is a proverb used to refer to the verb +.  There are
  54. also language elements called conjunctions, which join two verbs or
  55. proverbs, and there are elements called adverbs, which modify the
  56. meaning of one verb or proverb.
  57.  
  58.  
  59. Example 1:  Calculating e
  60.  
  61. This example shows how calculations are done with a list of numbers and
  62. how J uses a functional notation.  That is, each verb (function) acts on
  63. the noun or pronoun (data) to its right.   It is easy to create a
  64. collective (array).  Here, for instance, are 9 integers:
  65.  
  66.     i. 9
  67.  0 1 2 3 4 5 6 7 8
  68.  
  69.     a =. i. 9
  70.     a
  71.  0 1 2 3 4 5 6 7 8
  72.  
  73. The factorials of these numbers are:
  74.     ! a
  75.  1 1 2 6 24 120 720 5040 40320
  76.  
  77. The reciprocals of the above:
  78.     % ! a
  79.  1 1 0.5 0.166667 0.0416667 0.00833333 0.00138889 0.000198413 2.48016e_5
  80.  
  81. And the sum of reciprocals of the factorials of a is:
  82.     +/ % ! a
  83.  2.71828
  84.  
  85.  
  86. Those who know some mathematics may recognize an approximation to the
  87. number  e,  the base for the natural logarithms.  Of course, J has
  88. other ways to calculate this number; the point here is the use of the
  89. natural left-to-right execution of the meaning of the sequence symbols
  90. +/ % ! a  as "the sum of the reciprocals of the factorials of a".
  91.  
  92. Those who have done some programming in almost any other language, will
  93. see that the expression  +/%!i.9  is a remarkably short one for a function
  94. that produces the sum of the first few terms of the series that 
  95. approximates  e.  Moreover, it is possible to pronounce this
  96. program in English in a way that precisely conveys its meaning, and
  97. those who know the meaning of the words in the sentence will be able to
  98. understand it.  And anybody who knows the mathematics and who has 
  99. learned this much J will recognize that the program does exactly
  100. what the English words say and that it will produce an approximation
  101. to  e.
  102.  
  103. The author finds this expression in J much easier to think about and
  104. understand than the corresponding program intended to compute the same
  105. number in certain older languages.  Here, for example, is what one
  106. might write in Fortran, to accomplish the same result:
  107.  
  108.  
  109.     REAL SUM, FACT
  110.     SUM = 1.0
  111.     FACT = 1.0
  112.     DO 10 N = 1, 8
  113.         FACT = FACT * N
  114.         SUM = SUM + 1.0 / FACT
  115.      10 CONTINUE
  116.     PRINT, SUM
  117.     STOP
  118.     END
  119.  
  120.  
  121. Compare this Fortran program with the J program that uses only a few
  122. key strokes: +/ % ! i. 9 .  Not only is the J program shorter, but if
  123. you agree with the author that the program more closely approximates
  124. the English description, then you will probably agree that the J
  125. expression is easier to understand, even for someone who may have been
  126. using an older language, and who has only just learned the meaning of
  127. the symbols used in this example.  The use of this compact notation
  128. brings about much shorter programs.  It is the author's experience
  129. that for some applications, the ratio of the number of lines of
  130. Fortran or C code to the number of lines of J code has been about
  131. 20 to 1.
  132.  
  133.  
  134. Example 2:  Average
  135.  
  136. In this example, two verbs (functions) are defined.  As before, lines
  137. indented with four spaces were given as input during a J session, and
  138. the ones immediately after, with one leading space, were produced by
  139. the J interpreter.
  140.  
  141.  
  142.     Sum =. +/
  143.     Average =. Sum % #
  144.     c =. 1 2 3 4
  145.     Average c
  146.  2.5
  147.  
  148.  
  149. The first two lines create proverbs, the third creates a pronoun,
  150. and the fourth invokes the proverb (function) Average with the
  151. pronoun (data) c.  The meaning of   Average c  is this:
  152.  
  153.         (+/ % #) 1 2 3 4
  154.  
  155. and this may be thought of as:  find Sum c, ( +/ c means add up the
  156. entries in c), find # c (tally c), and then find the quotient of those
  157. two results.
  158.  
  159. The sequence of three functions used in the definition of Average
  160. provides an example of a fork.  A fork is a sequence of three functions
  161. (f h g), where
  162.     x (f g h) y  means  (x f y) g (x h y) .
  163. This diagram may help you to understand the meaning:
  164.  
  165.          g
  166.             / \
  167.            f   h
  168.           / \ / \
  169.           | | | |
  170.           x y x y
  171.  
  172. and the figure might suggest to you why the name fork is used.
  173. In a similar way, used with one argument,
  174.  
  175.   (f g h) y  means  (  f y) g (  h y).
  176.  
  177. Thus, looking again at Average c, we can see that this has the
  178. same meaning as (Sum c) % (tally c).  
  179.  
  180. If the above definition of fork strikes the reader as awkward, the
  181. reader is invited to consider a thought experiment about the meaning of
  182. the English phrase "a less than or equal to b".  Imagine that there is
  183. a function LT which returns the value 1 when a<b and 0 otherwise.
  184. Imagine a function EQ which returns the value 1 when a=b, and 0
  185. otherwise.  Imagine and a function called OR so that the expression x
  186. OR y returns 1 when x is 1 or y is 1.  Then the meaning of the fork
  187. (LT OR EQ) is understood by noting the equivalence of the statements:
  188.     x (LT OR EQ) y
  189.     (x LT y) OR (x EQ y).
  190.  
  191.  
  192.  
  193. Example 3:  Continued fractions
  194.  
  195. In this example, the verb  pr  (plus reciprocal), is considered.
  196.  
  197.     pr =. +%
  198.  
  199. The next two input lines show a use of this hook.  From them you
  200. should be able to see that the meaning of "a pr b" is "a plus the
  201. reciprocal of b", that is  5 +% 4 :
  202.  
  203.     5 pr 4
  204.  5.25
  205.  
  206. And this next example means 4 + % 5 :
  207.  
  208.     4 pr 5
  209.  4.2
  210.  
  211. The function pr is defined above as  +% , and is equivalent
  212. to 4 + (% 5).  This diagram for  4 +% 5   may help to understand
  213. its meaning.
  214.  
  215.                      +
  216.                         /  \
  217.                        4    %
  218.                             |
  219.                             5
  220.  
  221. Because of the shape of the diagram, the combination of two verbs
  222. in this way is called a hook.  In general, x (F G) y  means
  223. x F (G y) .
  224.  
  225. To continue with our example,
  226.  
  227.     1 pr 1 pr 1            
  228.  1.5
  229.  
  230. The above input line may be read as "1 plus the reciprocal
  231. of (1 plus the reciprocal of 1)".  And here below is an analogous
  232. line with four ones:
  233.  
  234.     1 pr 1 pr 1 pr 1
  235.  1.66667
  236.  
  237.     1 pr (1 pr (1 pr 1))
  238.  1.66667
  239.  
  240.     pr / 1 1 1 1        
  241.  1.66667
  242.  
  243.     pr / 4 $ 1
  244.  1.66667
  245.  
  246. The above input line has exactly the same meaning as the three input
  247. lines that come before it.  Notice that the value of the result is the
  248. same in each case.  The / is an adverb called insert and the result
  249. of  pr/  is a new verb, whose meaning is derived from the verb  pr.
  250.  
  251. In traditional mathematical notation the above expressions are written
  252.  
  253.  
  254.         1
  255.    1 + ------------------------
  256.         1
  257.     1 + ------------------
  258.             1
  259.         1 + ------------
  260.             1
  261.  
  262.  
  263. This particular continued fraction is a famous one because it is
  264. known to converge to the golden ratio.  That means that if you
  265. just keep on going, with more and more ones, the value gets closer and
  266. closer to the golden ratio.  One can see that the values of the continued
  267. fraction expansion seem to converge by using \, the prefix adverb.  It
  268. creates a sequence of partial results.  Here is a nice, compact
  269. expression that shows the values for the first 15 partial continued
  270. fractions.
  271.  
  272.     pr /\ 15$ 1
  273.  1 2 1.5 1.66667 1.6 1.625 1.61538 1.61905 1.61765 1.61818 1.61798
  274.       1.61806 1.61803 1.61804 1.61803
  275.  
  276. In this, you can see that the numbers seem to be getting closer and
  277. closer to some limit, probably between 1.61803 and 1.61804.  If you
  278. concentrate only on the odd numbered terms, (the first, the third, the
  279. fifth, and so on), you will see a sequence of numbers that is
  280. monotonically increasing.  On the other hand, the even numbered terms,
  281. ( 2, 1.66667, 1.625, and so on), form a sequence of numbers that is
  282. monotonically decreasing.  Of course these observations do not constitute
  283. a proof of convergence, but they might convince you that a proof can
  284. be found, and in the light of these observations, the proof probably
  285. follows along these lines.
  286.  
  287. Can you think of a simpler way to represent and evaluate continued
  288. fractions?  This author can not!
  289.  
  290. The golden ratio has been known for thousands of years, and is the
  291. number, bigger than one, such that itself minus 1 is its own reciprocal.
  292. The golden ratio is usually denoted by the lower case Greek letter tau.
  293. It is said that pictures whose sides are in the ratio 1 to tau
  294. or tau to 1 are the most pleasing to the eye.
  295.  
  296.  
  297. Example 4.  Summing data along different dimensions
  298.  
  299. In this example, we use some of the earlier functions to illustrate
  300. ideas about rank.  First some data are built.
  301.  
  302.     a =. 100 200
  303.     b =. 10 20 30
  304.     c =. 1 2 3 4
  305.  
  306. This is the beginning of the data construction.  Again, we use the verb
  307. Sum, a modification of the verb plus, defined as before:
  308.     Sum =. +/
  309. But in this example, the usage is different, because we use it with a
  310. right and a left argument.  To get an idea about the action of the verb
  311. Sum used dyadically (that is, used with two arguments), notice what Sum
  312. does with just b and c:
  313.  
  314.     b Sum c
  315.  11 12 13 14
  316.  21 22 23 24
  317.  31 32 33 34
  318.  
  319. Once you have seen this example, you are ready to see and understand
  320. the next step.  Here the collective  data  is built, using a, b,
  321. and c.
  322.  
  323.     data =. a Sum b Sum c
  324.     data
  325.  111 112 113 114
  326.  121 122 123 124
  327.  131 132 133 134
  328.  
  329.  211 212 213 214
  330.  221 222 223 224
  331.  231 232 233 234
  332.  
  333. This rank three array may be thought of as having has two planes,
  334. each having three rows and four columns.  The function $ (shape of)
  335. tells us the size of the array.
  336.  
  337.     $ data
  338.  2 3 4
  339.  
  340. Now examine what Sum does with the data:
  341.  
  342.     Sum data
  343.  322 324 326 328
  344.  342 344 346 348
  345.  362 364 366 368
  346.  
  347. Can you see what is happening?  It is adding up corresponding
  348. elements in the two planes.  Twelve different sums are performed,
  349. and the result is an array that is 3 by 4.  The action happens
  350. over the first dimension of the array.
  351.  
  352. But we might wish to add up numbers in rows.  We can do it this way:
  353.  
  354.     Sum "1 data
  355.  450 490 530
  356.  850 890 930
  357.  
  358. And to add up numbers in each column:
  359.  
  360.     Sum "2 data
  361.  363 366 369 372
  362.  663 666 669 672
  363.  
  364. The expressions "1 and "2 are read as 'rank 1' and 'rank 2'.  The rank
  365. 1 objects of 'data' are the rows, the rank 2 objects are the planes,
  366. and the rank 3 object is the whole object itself.  So, when one asks
  367. for  Sum"2 data ,  one asks for the action (Sum) to happen over the
  368. rank two arrays, and the action happens over the first dimension of
  369. these arrays.
  370.  
  371. Finally, the expression  Sum"3 data  has the same meaning as Sum data .
  372.  
  373. Now, recall the definition of Average.
  374.  
  375.     Average =. Sum % #
  376.  
  377. Apply the proverb Average to the pronoun data, to get:
  378.  
  379.     Average data
  380.  161 162 163 164
  381.  171 172 173 174
  382.  181 182 183 184
  383.  
  384. The result is the average of corresponding numbers in the two planes.
  385. That is, the average is calculated over the objects along the first
  386. dimension of data.  If we would like to know the average of the
  387. numbers in the rows, we type:
  388.  
  389.     Average "1 data
  390.  112.5 122.5 132.5
  391.  212.5 222.5 232.5
  392.  
  393. and finally, averaging the columns of the two planes:
  394.  
  395.     Average "2 data
  396.  121 122 123 124
  397.  221 222 223 224
  398.  
  399.  
  400. Again, compare the sizes of these results with the sizes of
  401. the results above where we were asking simply about sums.
  402. The verb $ (shape of) will help us here:
  403.  
  404.     Sum "1 data
  405.  450 490 530
  406.  850 890 930
  407.  
  408.     $ Sum "1 data
  409.  2 3
  410.     $ Average "1 data
  411.  2 3
  412.  
  413.     $ Sum "2 data
  414.  2 4
  415.     $ Average "2 data
  416.  2 4
  417.  
  418.  
  419. What the author finds exciting about this example is not just how
  420. easily the nouns and verbs were built, but how every verb acts in a
  421. uniform and predictable way on the data.  Rank one action is the same
  422. kind of action whether one is summing, averaging, or whatever.  It
  423. has been said that the concept of rank is one of the top ten computer
  424. inventions of all time.  [Monardo, 1991]
  425.  
  426.     UseNet Article in the news group comp.lang.apl, by
  427.     Pat Monardo of Cold Spring Harbor Laboratory,
  428.     Message-id: <1991May17.182034.5515@cshl.org>.
  429.  
  430.     ...  i also believe that the rank operator is a wonderful
  431.     invention, up there in the top ten computer inventions of all time.
  432.  
  433. Conclusion:
  434.  
  435. In this article, only a few of the many verbs in J have been used, but
  436. the reader has had a glimpse some of the power of J, as seen in hooks
  437. and forks, in the implicit definition of verbs, and in the uniform way
  438. that J acts on sub-arrays by means of the rank operator.  J is powerful
  439. because one can get results quickly.  It is a carefully thought out and
  440. beautifully consistent programming language for people who need to
  441. write programs for themselves, but who don't want to spend inordinately
  442. large amounts of time programming to get those results.  If you have to
  443. write programs for others, this could still be a language for you,
  444. especially if you get paid by the job, and not by time.
  445.  
  446.  
  447. Availability:
  448.  
  449. J is available by anonymous ftp from watserv1.waterloo.edu in the
  450. directory "~/languages/apl/j" and is available from certain other servers.
  451. At the Waterloo site, versions of J are available that run on several
  452. kinds of computers.  J is a product of Iverson Software Inc., 33 Major
  453. Street, Toronto, Ontario, Canada  M5S 2K9.  (416) 925 6096, and when
  454. purchased from them, comes with a dictionary of J and other aids
  455. for learning J.
  456.  
  457.  
  458. Learning J:
  459.  
  460. Because J is a relatively new language, there are not yet many books or
  461. articles about it.  At the Waterloo site, there is a tutorial that has
  462. much material concentrated into a small space.   With each release
  463. of J to date, there has been a status file that tells which features
  464. have been implemented.  Several status files are available.
  465.  
  466. The reader who chooses to learn J from these materials might like to
  467. print the 47 tutorial files and study them one at a time, and to have
  468. at hand a copy of a status file listing the names of the verbs.  For
  469. each example given in the tutorial, the reader might wish to experiment
  470. with J by inventing nouns, verbs, adverbs, and conjunctions to test
  471. ideas related to those presented in the tutorial and thereby to let J
  472. itself be the guide and the interpreter (pun intended).
  473.  
  474.  
  475. The author of J:
  476.  
  477. The inventor-developer of J is Kenneth E. Iverson, the same man who
  478. invented APL, a pioneer of computing, and recipient of the prestigious
  479. ACM Turing Award.  Some years ago, he retired from IBM and went to work
  480. for I.P.Sharp Associates, a Toronto company that developed the world's
  481. first private packet switching network, the finest APL available, and
  482. was at one time the world's largest time sharing service which just
  483. happened to be devoted entirely to APL.  The descendent of this
  484. company, Reuter:file, still has offices in many of the world's major
  485. cities.  Now Iverson is retired (again) and lives in Toronto, where he
  486. spends his time developing and promoting J.
  487.  
  488. The implementor for J is Roger Hui.  Roger hails from Edmonton, and has
  489. lived in Toronto since he joined I.P.Sharp Associates in 1975.  The
  490. source code for J, written in C, is worthy of some comment, because it
  491. makes extensive use of macro expansion.  Its style, influenced by the
  492. work of Arthur Whitney of Morgan Stanley in New York, reflects Roger's
  493. strong background in APL.  Today, Roger spends much of his time
  494. developing J and does occasional contract work in APL.
  495.  
  496.  
  497. Copyright (c) 1991,1992 by Leroy J. Dickey
  498. Permission is granted to use this for
  499. nonprofit and educational purposes.
  500. It may be given away, but it may not be sold.
  501.  
  502.  
  503. Revised 1992-03-21.
  504.  
  505.