J Frequently Asked Questions --------------------------------------------------------------------- Internet discussion group: comp.lang.apl Comments and suggestions: cdburke@aol.com (Chris Burke) --------------------------------------------------------------------- Topics discussed here: 1. What is J? 2. What is the current version? 3. What is the documentation? 4. What platforms are supported? 5. What systems are available? 6. Where can I obtain J? 7. What are the pricing and licensing arrangements? 8. What is the Web site? 9. What books and papers are there on J? 10. How do I go about learning J? Appendix 1. Language Overview 2. Examples 3. Beginners' Questions --------------------------------------------------------------------- (1) What is J? J is a very high level general-purpose language, with a strong emphasis on functional programming and array processing. J was designed and developed by Ken Iverson and Roger Hui, and implemented by Iverson Software Inc (ISI). J is distinguished by its simple and consistent rules, a large set of built-in functions, powerful facilities for defining new operations, and a general and systematic treatment of arrays. It is ideal for complex analytical work, modelling, and rapid application development. It is available on most popular hardware platforms, and the script files used to represent programs are completely portable. Only ASCII characters are used, and there are no reserved words: primitives are represented by single characters (such as + and <), or by a character followed by a period (+. for Boolean OR and <. for lesser-of, or minimum), or by a colon (+: for not-OR and <: for less than or equal). Vectors (lists), matrices (tables), and higher-rank arrays are treated as single entities. Thus a+b adds a to b, whether they are single numbers, lists or tables. --------------------------------------------------------------------- (2) What is the current version? The current system is "J Release 3" (J3). The current version number is 3.01 (February 1996). The previous major release was J2 in September 1994. Prior to that, J was a shareware product with the last version being version 7. Earlier versions are no longer distributed or supported. --------------------------------------------------------------------- (3) What is the documentation? There are three manuals: 1. Introduction and Dictionary This is the reference manual for J, and includes introductory tutorials on various aspects of the language. 2. User Manual This manual explains the application development environment and J utility programs that are included with the Windows version, and includes sample applications and templates. 3. J Phrases This book contains several hundred phrases useful to beginners in learning the language as well as to experienced programmers. The full text of these manuals is contained in help files distributed with the Windows and Macintosh versions. --------------------------------------------------------------------- (4) What platforms are supported? J is available on Windows 95/NT, Windows 3.1, DOS 386, Linux, Mac, RS/6000 and Sparc. The Windows 3.1 runs in Windows under OS/2. The core language is the same on all platforms; programs not making use of platform-dependent features will work unchanged on all systems. In each case, the language is a full 32 bit implementation with no limit on object size. An application development environment is currently included only with the Windows and Mac versions, while other systems consist solely of the J language. J for Windows 95/NT ------------------- J is a true Windows 95/NT product that supports standard Windows classes (button, editbox, richedit etc.) as well as a graphics class with full GDI support, a picture class, ownerdraw buttons and listboxes. J can be used as an OLE Automation server: The JEXEServer is the full J development system, giving full access to both the client and the J server environments. The JDLLServer is the J interpreter only, accessed as an in-process server. This is very efficient and is ideal for runtime applications. The J DLL can also be called directly as an ordinary 32-bit DLL without using OLE, by any application capable of calling 32-bit DLLs. J supports 32-bit ODBC database access, with drivers for most popular databases, and DDE. J can call procedures in 32-bit DLLs. Requirements: Windows 95 or NT, 8 MB RAM (more is better), 8 MB hard disk. J for Windows 3.1 ----------------- This is similar to J for Windows 95/NT, but without Windows 95/NT specific controls, and without OLE support. Database access is 16-bit ODBC, and DLL calls are to 16-bit DLLs. Requirements: Windows 3.1 or later, 8 MB RAM (more is better), 8 MB hard disk. This product also runs under the Windows 3.1 capability of Windows 95/NT and OS/2. J for Mac --------- This is a port of the Windows product including essentially the same development environment. Requirements: Mac System 7, 8 MB RAM (more is better), 8 MB hard disk. J for DOS --------- The system requires a 386 or better, and uses extended memory. A coprocessor is not required, but is used if available. J for Linux ----------- The system includes the interpreter only, with no session manager. J for RS/6000 ------------- The system includes the interpreter only, with no session manager. J for Sparc ----------- The system includes the interpreter only, with no session manager. The distribution includes executables for both SunOS 4.1 and SunOS 5.3 (Solaris). --------------------------------------------------------------------- (4) What systems are available? J is available in 3 development editions, and as a runtime system. Professional and Standard editions are commercial products. FreeWare and Runtime are freely available. Development Editions ------------------- Professional: Windows 95/NT Windows 3.1 Standard: Windows 95/NT Windows 3.1 DOS 386 Macintosh UNIX: Linux, SUN, RS/6000 FreeWare: Windows 3.1 Runtime Systems --------------- Windows 95/NT - full executable Windows 95/NT - language DLL only Windows 3.1 - full executable Windows systems are distinguished as follows: Professional editions include the 3 manuals, and are able to create encoded script files (allowing you to package an application with no access to the source code). Standard editions include only the Introduction and Dictionary, and the User Manual. They cannot create encoded script files, and lack OLE, ODBC and DLL facilities. The FreeWare edition is for Windows 3.1 only, uses only the coprocessor emulator, and lacks DDE and some code optimizations. This edition is ideal as a demo copy and for learning the language, but runs much slower than other editions. --------------------------------------------------------------------- (6) Where can I obtain J? J is distributed by Strand Software Systems, and other authorized dealers. For information, contact: Anne Faust email: sales@jsoftware.com Strand Software 19235 Covington Court Shorewood, Minnesota 55331 tel: 612-470-7345 fax: 612-470-9202 Local dealers: Sylvain Baron Sylicom 184, rue Marcel Hartmann 94200 IVRY Paris, France Tel: 331 4521 9850 Fax: 331 4521 9851 Gert Osterburg Technoma Gmbh AM Hochholz 7 Karben 5 FRG 61184 Tel: 6034 2995 Fraser Jackson MasterWork Software Ltd Postal Address: Delivery Address: PO Box 56-036 77 Larsen Crescent Tawa Tawa Wellington Wellington New Zealand New Zealand Tel: +64(4) 232-4440 Fax: +64(4) 232-4440 Richard Hill Adaptable Systems 49 First Street Black Rock, Victoria 3193 Australia Tel: 613 9589 5578 Fax: 613 9589 3220 Email: hillrj@ibm.net Bjorn Helgason Fugl & Fiskur hf Spitalastig 4 101 Reykjavik Iceland Tel: 354-562-5441 or 354-550-6462 Email: bjornhp@simi.is Joachim Hoffmann Muenzgrabenstrasse 68 / 4 A-8010 Graz Austria Tel: 43 316 81 45 29 Fax: 43 316 68 42 43 --------------------------------------------------------------------- (7) What are the pricing and licensing arrangements? Single copy prices in North America are: Professional: $495 (includes 3 manuals) Standard: $150 (includes Intro. & Dict., User Manual) Standard (diskettes only) $80 Manuals: $40 each Prices are in US$, do not include shipping and handling, and are applicable only in North America. Higher prices apply elsewhere. Teachers using J as part of a course may use a Standard system which may be installed on all computers in the course lab. Students needing their own copy should obtain the FreeWare version. The Educational price of a Professional system is 60% of the regular price. Network and site licenses are available, and the source is available for users who wish to compile J on machines not currently supported. Contact Strand Software for details. --------------------------------------------------------------------- (8) What is on the Web site? Our Web page is http://www.jsoftware.com You can download the full text of the manuals, in Windows Help file format, J FreeWare, J Runtime, the current J scripts, and J Extras, a compilation of utility scripts. J files may also be found at the University of Waterloo: watserv1.uwaterloo.ca ftp://watserv1.uwaterloo.ca/languages/j/Welcome.html and the European mirror site: wuvieai.wu-wien.ac.at (137.208.15.20) --------------------------------------------------------------------- (9) What books and papers are there on J? Published by ISI: Arithmetic (Ken Iverson) Calculus (Ken Iverson) Concrete Math Companion (Ken Iverson) Fractals Visualization and J (Clifford Reiter) Others: Introduction to J (Harvey Davies) Some notes on introducing J with statistical examples (Keith Smillie) Many articles on J can be found in: VECTOR - British APL Association Don McIntyre has written several articles in Vector, including a 2 part article "Learning J". Gimme Arrays! - Toronto APL SIG Articles include: Functional Programming in J for Operations Research (Richard Brown) Chinese Rings in J (Lee Dickey) APL Quote Quad, including APL Conference Proceedings (ACM) The Conference Proceedings since 1990 contain papers on J. IBM Systems Journal APL Special Edition Vol 30 #4, 1991 Contains articles on APL and J describing their evolution and various implementations. --------------------------------------------------------------------- (10) How do I go about learning J? Where possible, you should obtain a full version of J, which will include support for developing J applications and a suite of utility programs. Even if you intend to run J on another system, you will likely find a full version the best way to learn J. For learning J, you can download the FreeWare version. You need the "J Introduction and Dictionary", which is an essential reference for learning J, and the "J User Manual", which describes the user interface and utility programs. J is a very high level language and learning it can be daunting. The key point is that knowing even the smallest and simplest parts of J can provide you with powerful programming tools. This core can be learned with relatively little effort and may be adequate to your needs. You may never need to learn any more about J. On the other hand, every bit of additional effort in mastering J gives you enormous leverage in solving complex data processing problems. Start by working through the examples in the User Manual Chapter 2, then read Chapter 3 describing the J session manager. Next, work your way through parts of the Introduction and Dictionary. Some parts you should study in detail, some parts you should skim, and some you can ignore until you become interested and motivated by your applications. Note that the Dictionary is a reference, and is not intended to be read from cover to cover. Reading the first few sections of the Introduction with access to the J system, try the examples, experiment and explore. Read sections: Mnemonics, Ambivalence, Verbs and Adverbs, Punctuation, Programs, Vocabulary, Housekeeping, Word Formation, and Explicit Definition. Skim the other sections in the Introduction half of the manual. Skip material that is complicated or not immediately relevant. Read the Dictionary Sections I and II, up to where the definitions of the individual primitives start. Again, it is not important or expected that everything makes sense at this early stage. Become familiar with using the vocabulary on the back cover. For example, find the row for *, and refer to the pages with the definitions of * and *. and *: . --------------------------------------------------------------------- Appendix (1) Language Overview The following is excerpted from the Introduction and Dictionary: J is a general-purpose programming language available on a wide variety of computers. Although it has a simple structure and is readily learned by anyone familiar with mathematical notions and notation, its distinctive features may make it difficult for anyone familiar with more conventional programming languages. Aspects of J that distinguish it from other languages include: 1. A mnemonic one- or two-character spelling for primitives. 2. No order-of-execution hierarchy among functions. 3. The systematic use of ambivalent functions that, like the minus sign in arithmetic, can denote one function when used with two arguments (subtraction in the case of -), and another when used with one argument (negation in the case of -). 4. The adoption of terms from English grammar that better fit the grammar of J than do the terms commonly used in mathematics and in programming languages. Thus, a function such as addition is also called a verb (because it performs an action), and an entity that modifies a verb (not available in most programming languages) is accordingly called an adverb. 5. The systematic use of adverbs and conjunctions to modify verbs, so as to provide a rich set of operations based upon a rather small set of verbs. For example, +/a denotes the sum over a list a, and */a denotes the product over a, and a */ b is the multiplication table of a and b. 6. The treatment of vectors, matrices, and other arrays as single entities. 7. The use of functional or tacit programming that requires no explicit mention of the arguments of a function (program) being defined, and the use of assignment to assign names to functions (as in sum=.+/ and mean=.sum % #). I. ALPHABET and WORDS The alphabet is standard ASCII, comprising digits, letters (of the English alphabet), the underline (used in names and numbers), the (single) quote, and others (which include the space) to be referred to as graphics. Alternative spellings for the national use characters (which differ from country to country) appear on page 180. Numbers are denoted by digits, the underbar (for negative signs and for infinity and minus infinity when used alone or in pairs), the period (used for decimal points and necessarily preceded by one or more digits), the letter e (as in 2.4e3 to signify 2400 in exponential form), and the letter j to separate the real and imaginary parts of a complex number, as in 3e4j_0.56. Also see page 201. A numeric list or vector is denoted by a list of numbers separated by spaces. A list of ASCII characters is denoted by the list enclosed in single quotes, a pair of adjacent single quotes signifying the quote itself: 'can''t' is the five-character abbreviation of the six-character word 'cannot'. The ace a: denotes the boxed empty list <$0 . Names (used for pronouns and other surrogates, and assigned referents by the copula, as in prices=. 4.5 12) begin with a letter and may continue with letters, underlines, and digits. A name that ends with an underline is a locative, as discussed in Section II.H. A primitive or primary may be denoted by any single graphic (such as + for plus) or by any graphic modified by one or more following inflections (a period or colon), as in +. and +: for or and nor. A primary may also be an inflected name, as in e. and o. for membership and pi times. A primary cannot be assigned a referent by a copula. II. GRAMMAR The following sentences illustrate the six parts of speech: fahrenheit=. 50 (fahrenheit-32)*5%9 10 prices=. 3 1 4 2 orders=. 2 0 2 1 orders * prices 6 0 8 2 PARTS of SPEECH +/orders*prices 16 50 fahrenheit Nouns/Pronouns +/\1 2 3 4 5 + - * % bump Verbs/Proverbs 1 3 6 10 15 / \ Adverbs bump=. 1&+ & Conjunction bump prices ( ) Punctuation 4 2 5 3 =. Copula Verbs act upon nouns to produce noun results; the nouns to which a particular verb applies are called its arguments. A verb may have two distinct (but usually related) meanings according to whether it is applied to one argument (to its right), or to two arguments (left and right). For example, 2%5 yields 0.4, and %5 yields 0.2. An adverb acts on a single noun or verb to its left. For example, +/ is a derived verb (which might be called plus over) that sums an argument list to which it is applied, and */ yields the product of a list. A conjunction applies to two arguments, either nouns or verbs. Punctuation is provided by parentheses that specify the sequence of execution as in elementary algebra. The word =. behaves like the copulas is and are in English, and is read as such, as in area is 3 times 4 for area=. 3*4. The name area thus assigned is a pronoun and, as in English, it plays the role of a noun. Similar remarks apply to names assigned to verbs, adverbs, and conjunctions. Entry of a name alone displays its value. Errors are discussed in Section I. A. NOUNS Nouns are classified in three independent ways: numeric or literal; open or boxed; arrays of various ranks. In particular, arrays of ranks 0, 1, and 2 are called atom, list, and table, or, in mathematics, scalar, vector, and matrix. Numbers and literals are represented as stated in Part I. Arrays. A single entity such as 2.3 or _2.3j5 or 'A' or '+' is called an atom. The verb denoted by comma chains its arguments to form a list whose shape (given by the verb $) is equal to the number of atoms combined. For example: $ date=. 1,7,7,6 4 word=. 's','a','w' |. word |. date was 6 7 7 1 The verb |. used above is called reverse. The phrase s$b produces an array of shape s from the list b. For example: (3,4) $ date,1,8,6,7,1,9,1,7 1 7 7 6 1 8 6 7 1 9 1 7 table=. 2 3$ word,'bat' table $table saw 2 3 bat The number of atoms in the shape of a noun is called its rank. Each position of the shape is called an axis of the array, and axes are referred to by indices 0, 1, 2, etc. For example, axis 0 of table has length 2 and axis 1 has length 3. The last k axes of an array b determine rank-k cells or k-cells of b. The rest of the shape vector is called the frame of b relative to the cells of rank k; if $c is 2 3 4 5, then c has the frame 2 3 relative to cells of rank 2, the frame 2 3 4 5 relative to 0-cells (atoms), and an empty frame relative to 4-cells. If: ] b=.2 3 4 $ 'abcdefghijklmnopqrstuvwx' abcd efgh ijkl mnop qrst uvwx then the list abcd is a 1-cell of b, and the letters are each 0-cells. A cell of rank one less than the rank of b is called an item of b; an atom has one item, itself. For example, the verb from (denoted by {) selects items from its right argument, as in: 0{b 1{b 0{0{b abcd mnop abcd efgh qrst ijkl uvwx 2 1{0{b 1{2{0{b 0{3 ijkl j 3 efgh Moreover, the verb grade (denoted by /:) provides indices to { that bring items to lexical order. Thus: g=. /: n=. 4 3$3 1 4 2 7 9 3 2 0 n g g{n 3 1 4 1 0 3 2 2 7 9 2 7 9 3 1 4 3 2 0 3 1 4 3 1 4 3 2 0 Negative numbers, as in _2-cell and _1-cell (an item), are also used to refer to cells whose frames are of the length indicated by the magnitude of the number. For example, the list abcd may be referred to either as a _2-cell or as a 1-cell of b. Open and Boxed. The nouns discussed thus far are called open, to distinguish them from boxed nouns produced by the verb box denoted by < . The result of box is an atom, and boxed nouns are displayed in boxes. Box allows one to treat any array (such as the list of letters that represent a word) as a single entity, or atom. Thus: words=.(<'I'),(<'was'),(<'it') letters=. 'I was it' $words $letters 3 8 |. words |. letters +--+---+-+ ti saw I |it|was|I| +--+---+-+ 2 3$words,|.words +--+---+--+ |I |was|it| +--+---+--+ |it|was|I | +--+---+--+ B. VERBS Monads and Dyads. Verbs have two definitions, one for the monadic case (one argument), and one for the dyadic case. The dyadic definition applies if the verb is preceded by a suitable left argument, that is, any noun that is not itself an argument of a conjunction; otherwise the monadic definition applies. The monadic case of a verb is also called a monad, and we speak of the monad % used in the phrase %4, and of the dyad % used in 3%4. Either or both cases may have empty domains. Ranks of Verbs. The notion of verb rank is closely related to that of noun rank: a verb of rank k applies to each of the k-cells of its argument. For example (using the array b from Section A): ,b abcdefghijklmnopqrstuvwx ,"2 b ,"_1 b abcdefghijkl abcdefghijkl mnopqrstuvwx mnopqrstuvwx Since the verb ravel (denoted by ,) applies to its entire argument, its rank is said to be unbounded. The rank conjunction " used in the phrase ,"2 produces a related verb of rank 2 that ravels each of the 2-cells to produce a result of shape 2 by 12. The shape of a result is the frame (relative to the cells to which the verb applies) catenated with the shape produced by applying the verb to the individual cells. Commonly these individual shapes agree, but if not, they are first brought to a common rank by adding leading unit axes to any of lower rank, and are then brought to a common shape by padding with an appropriate fill element: space for a character array, 0 for a numeric array, and a boxed empty list for a boxed array. For example: i."0 s=. 2 3 4 >'I';'was';'here' 0 1 0 0 I 0 1 2 0 was 0 1 2 3 here The dyadic case of a verb has two ranks, governing the left and right arguments. For example: p=. 'abc' q=. 3 5$'wake read lamp ' p,"0 1 q awake bread clamp Finally, each verb has three intrinsic ranks: monadic, left, and right. The definition of any verb need specify only its behaviour on cells of the intrinsic ranks, and the extension to arguments of higher rank occurs systematically. The ranks of a verb merely place upper limits on the ranks of the cells to which it applies, and its domain may include arguments of lower rank. For example, matrix inverse (%.) has monadic rank 2, but treats degenerate cases of vector and scalar arguments as one-column matrices. Agreement. In the phrase p v q, the arguments of v must agree in the sense that their frames (relative to the ranks of v) must be a prefix of the other, as in p,"0 1 q above, and in the following examples: p," 1 1 q 3 4 5*i. 3 4 abcwake 0 3 6 9 abcread 16 20 24 28 abclamp 40 45 50 55 (i.3 4)*3 4 5 0 3 6 9 16 20 24 28 40 45 50 55 If a frame contains 0, the verb is applied to a cell of fills. For example: ($ #"2 i. 1 0 3 4);($ 2 3 %"1 i. 0 2) +---+---+ |1 0|0 2| +---+---+ ($ $"2 i. 1 0 3 4);($ 2 3 %/"1 i. 0 4) +-----+-----+ |1 0 2|0 2 4| +-----+-----+ C. ADVERBS AND CONJUNCTIONS Unlike verbs, adverbs and conjunctions have fixed valence: an adverb is monadic (applying to a single argument to its left), and a conjunction is dyadic. Conjunctions and adverbs apply to noun or verb arguments; a conjunction may produce as many as four distinct classes of results. For example, u&v produces a composition of the verbs u and v; and ^&2 produces the square by bonding the power function with the right argument 2; and 2&^ produces the function 2-to-the-power. The conjunction & may therefore be referred to by different names for the different cases, or it may be referred to by the single term and (or with), which roughly covers all cases. D. COMPARATIVES The comparison x=y is treated like the everyday use of equality (that is, with a reasonable relative tolerance), yielding 1 if the difference x-y falls relatively close to zero. polerant comparison also applies to other relations and to floor and ceiling (<. and >.); a precise definition is given in Part III under equal ( =). An arbitrary tolerance t can be specified by using the fit conjunction (!.), as in x =!.t y. E. PARSING & EXECUTION A sentence is evaluated by executing its phrases in a sequence determined by the parsing rules of the language. For example, in the sentence 10%3+2, the phrase 3+2 is evaluated first to obtain a result that is then used to divide 10. In summary: 1. Execution proceeds from right to left, except that when a right parenthesis is encountered, the segment enclosed by it and its matching left parenthesis is executed, and its result replaces the entire segment and its enclosing parentheses. 2. Adverbs and conjunctions are executed before verbs; the phrase ,"2-a is equivalent to (,"2)-a, not to ,"(2-a). Moreover, the left argument of an adverb or conjunction is the entire verb phrase that precedes it. phus, in the phrase +/ . */b, the rightmost adverb / applies to the verb derived from the phrase +/ . *, not to the verb *. 3. A verb is applied dyadically if possible; that is, if preceded by a noun that is not itself the right argument of a conjunction. 4. Certain trains form verbs, adverbs, and conjunctions, as described in F. 5. To ensure that these summary parsing rules agree with the precise parsing rules prescribed below, it may be necessary to parenthesize any adverbial or conjunctival phrase that produces anything other than a noun or verb. One important consequence of these rules is that in an unparenthesized expression the right argument of any verb is the result of the entire phrase to the right of it. The sentence 3*p%q^|r-5 can therefore be read from left to right: the overall result is 3 times the result of the remaining phrase, which is the quotient of p and the part following the %, and so on. Parsing proceeds by moving successive elements (or their values in the case of pronouns and other names) from the tail end of a queue (initially the original sentence prefixed by a left marker §) to the top of a stack, and eventually executing some eligible portion of the stack and replacing it by the result of the execution. For example, if a=. 1 2 3, then b=.+/2*a would be parsed and executed as follows: $ b =. + / 2 * a $ b =. + / 2 * 1 2 3 $ b =. + / 2 * 1 2 3 $ b =. + / 2 * 1 2 3 $ b =. + / 2 * 1 2 3 $ b =. + / 2 4 6 $ b =. + / 2 4 6 $ b =. + / 2 4 6 $ b =. 12 $ b =. 12 $ 12 $ 12 The foregoing illustrates two points: 1) Execution of the phrase 2*1 2 3 is deferred until the next element (the /) is transferred; had it been a conjunction, the 2 would have been its argument, and the monad * would have applied to 1 2 3; and 2) Whereas the value of the name a moves to the stack, the name b (because it precedes a copula) moves unchanged, and the pronoun b is assigned the value 12. F. TRAINS An isolated sequence, such as (+ */), which the normal parsing rules (other than the three labelled trident and bident) do not resolve to a single part of speech is called a train, and may be further resolved as described below. Meanings are assigned to certain trains of two or three elements and, by implication, to trains of any length by repeated resolution. For example, the trains +-*% and +-*%^ are equivalent to +(-*%) and +-(*%^). A verb is produced by trains of three or two verbs, as defined by the following diagrams: FORK HOOK g g g g / \ / \ / \ / \ f h f h y h x h | | / \ / \ | | y y x y x y y y For example, 5(+*-)3 is (5+3)*(5-3), but if f is a cap ([:) the capped branch simplifies the forks to g h y and g x h y. The ranks of the hook and fork are infinite. --------------------------------------------------------------------- Appendix(2) Examples The following shows a typical J session. User entries are indented 3 spaces, and NB. denotes a comment. Boxed output is shown here with ordinary characters, but where available, true line-drawing characters may be used. NB. calculate sums and means: count=. # sum=. +/ mean=. sum % count mean 2 3 5 7 4.25 report=. i. 2 3 4 (] ; sum ; sum"2 ; sum"1) report +-----------+-----------+-----------+--------+ | 0 1 2 3|12 14 16 18|12 15 18 21| 6 22 38| | 4 5 6 7|20 22 24 26|48 51 54 57|54 70 86| | 8 9 10 11|28 30 32 34| | | | | | | | |12 13 14 15| | | | |16 17 18 19| | | | |20 21 22 23| | | | +-----------+-----------+-----------+--------+ (] ; mean ; mean"2 ; mean"1) report +-----------+-----------+-----------+--------------+ | 0 1 2 3| 6 7 8 9| 4 5 6 7| 1.5 5.5 9.5| | 4 5 6 7|10 11 12 13|16 17 18 19|13.5 17.5 21.5| | 8 9 10 11|14 15 16 17| | | | | | | | |12 13 14 15| | | | |16 17 18 19| | | | |20 21 22 23| | | | +-----------+-----------+-----------+--------------+ NB. allocate sales by item number: items=. 1 2 2 3 2 3 1 1 2 sales=. 20 25 30 30 15 20 10 15 15 items 'ca';'cat' +--+---+ |ca|cat| |ac|cta| | |act| | |atc| | |tca| | |tac| +--+---+ NB. calculate decrement rates from population: d1=. [: -. }.%}: d2=. 2&((-.@%~)/\) d3=. -.@}.@(*/\^:_1) pop=. 100 90 72 48 24 9 2 0 d1 pop 0.1 0.2 0.333333 0.5 0.625 0.777778 1 d2 pop 0.1 0.2 0.333333 0.5 0.625 0.777778 1 (d2 -: d3) pop 1 NB. sorted nub of word list: sort=. /:~ open=. > nub=. ~. words=. ;: text=. 'seven maids with seven mops' sort open nub words text maids mops seven with NB. fit 3rd order polynomial to data: x=. _1 _0.5 0 0.5 1 y=. _2 2 1 _1 0.5 f=. (y %. x ^/ i.4)&p. f x _2.00714 2.02857 0.957143 _0.971429 0.492857 f i.5 0.957143 0.492857 30.6 125.279 318.529 f +----------------------------------+-+--+ |0.957143 _4.41667 _1.71429 5.66667|&|p.| +----------------------------------+-+--+ NB. fund accumulation with interest: int=. 0.1 0.1 0.1 0.05 0.05 pay=. 100 120 120 150 200 200 accum=. 1,*/\1+int proj=. +/\&.(%&accum) proj pay 100 230 373 560.3 788.315 1027.73 proj 6#100 100 210 331 464.1 587.305 716.67 NB. continued fraction representation of Pi: rf=. % @ (1&|) Pi=. 1p1 [v=. <. rf ^: (i.10) Pi 3 7 15 1 292 1 1 1 2 1 (+%) /\ 5{. v 3 3.14286 3.14151 3.14159 3.14159 NB. signed area (or volume): area=. vol=. [: det ] ,. %@!@#"1 det=. -/ . * tri=. 0 0,1 0,:3 1 tri;(area tri);(|.tri);(area |.tri) +---+---+---+----+ |0 0|0.5|3 1|_0.5| |1 0| |1 0| | |3 1| |0 0| | +---+---+---+----+ tetrahedron=. 0,=i.3 tetrahedron; vol tetrahedron +-----+---------+ |0 0 0|_0.166667| |1 0 0| | |0 1 0| | |0 0 1| | +-----+---------+ ------------------------------------------------------------------ Appendix(3) Beginners' Questions Many features of J are either not present at all, or are very different in other languages. Such features are often stumbling blocks for newcomers to J. In particular, questions are frequently asked about: rank order of execution indexing The following is an informal presentation of these topics with typical examples. You should look up the references to the Introduction and Dictionary for exact definitions and terminology. Rank ---- Rank is described in the Introduction lesson on rank, the Dictionary Part IIA and IIB, and the Dictionary entry for rank. A good understanding of rank is essential for using J. Rank specifies the behaviour of a verb on certain subarrays of its arguments, which for a rank-k verb are referred to as the k-cells. Each verb is assigned a rank, and a rank may be otherwise specified with the rank conjunction " . The verb < (box) is useful for illustrating rank. < boxes its argument, and has unbounded rank, which means it boxes its entire argument: [n=. i.2 3 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1 2;10 20 NB. add up each item of n 3 30 NB. rank 0, since > has rank 0 +/@:> 1 2;10 20 NB. add up items of n (rank _) 11 22 You can find out the rank of a verb using the adverb b. (basic characteristics). The result shows the monadic, dyadic left and dyadic right ranks: +/ b. 0 _ _ _ +/@> b. 0 0 0 0 +/@:> b. 0 _ _ _ Here rank is used to specify how two arrays are joined together: [a=.'XYZ' XYZ [b=. >;:'bat cat dat' bat cat dat a,b NB. append a to b XYZ bat cat dat a,"1 1 b NB. append rows of a to rows of b XYZbat XYZcat XYZdat a,"0 1 b NB. append atoms of a to rows of b Xbat Ycat Zdat a,"1 0 b NB. append rows of a to atoms of b XYZb XYZa XYZt XYZc XYZa XYZt XYZd XYZa XYZt a,"0 0 b NB. append atoms of a to atoms of b Xb Xa Xt Yc Ya Yt Zd Za Zt Order of execution ------------------ J follows the simple rules specified in the Dictionary Part IIE (see Appendix 1 of this FAQ). Unlike other languages, there is no precedence between verbs, or between adverbs and conjunctions. Like other languages, parentheses can be used to specify the sequence of execution. You can often solve problems with precedence by adding parentheses to an expression: 9-3-4 NB. equivalent to 9-(3-4) 10 (9-3)-4 NB. parentheses change order of execution 2 +/ }. 1 2 3 NB. (1) takes sum of the beheaded list 5 (+/ }.) 1 2 3 NB. (2) applies the hook +/ }. to 1 2 3 3 4 NB. i.e. the expression in parentheses 4 5 NB. is executed first 5 6 a=. +/ }. NB. is the same as (2), not (1), since a 1 2 3 NB. the order of execution is given 3 4 4 5 5 6 Boxed display is useful for showing the order of execution: +/ . * / +-----------+-+ NB. shows that the rightmost adverb applies to |+-----+-+-+|/| NB. the derived verb +/ . * and that ||+-+-+|.|*|| | NB. the left argument of the dot product |||+|/|| | || | NB. is the derived verb +/ ||+-+-+| | || | |+-----+-+-+| | +-----------+-+ Indexing -------- Indexing is effected using the verb { (from), and indexed replacement using the adverb } (amend). From and amend are symmetric. These are described in entries in the Dictionary. From and amend provide all the functionality of bracket indexing found in other languages, and much more. Examples of from: [m=. 4 5$'cabletreatbraidrider' cable treat braid rider 2{m NB. pick the 2nd row braid 1 2{"1 m NB. pick columns 1 2 ab re ra id (<2 1){m NB. pick the element in row 2, column 1 r (0 3;2 2;2 3;3 0){m NB. scattered indexing lair Examples of amend: 'godel' 2 } m NB. replace row 2 cable treat godel rider ('xyzt',.'XYZT') 1 2}"1 m NB. replace columns 1 2 cxXle tyYat bzZid rtTer 'Z' (<2 1)}m NB. replace element in row 2, column 1 cable treat bZaid rider 'ABCD' (0 3;2 2;2 3;3 0)}m NB. scattered replacement cabAe treat brBCd Dider