J (programming language)
The J programming language, developed in the early 1990s by Kenneth E. Iverson and Roger Hui,[5][6] is an array programming language based primarily on APL (also by Iverson). To avoid repeating the APL special-character problem, J uses only the basic ASCII character set, resorting to the use of the dot and colon as inflections[7] to form short words similar to digraphs. Most such primary (or primitive) J words serve as mathematical symbols, with the dot or colon extending the meaning of the basic characters available. Also, many characters which in other languages often must be paired (such as J is a very terse array programming language, and is most suited to mathematical and statistical programming, especially when performing operations on matrices. It has also been used in extreme programming[8] and network performance analysis.[9] Like John Backus's languages FP and FL, J supports function-level programming via its tacit programming features. Unlike most languages that support object-oriented programming, J's flexible hierarchical namespace scheme (where every name exists in a specific locale) can be effectively used as a framework for both class-based and prototype-based object-oriented programming. Since March 2011, J is free and open-source software under the GNU General Public License version 3 (GPLv3).[10][11][12] One may also purchase source under a negotiated license.[13] ExamplesJ permits point-free style and function composition. Thus, its programs can be very terse and are considered difficult to read by some programmers. The "Hello, World!" program in J is: 'Hello, World!'
This implementation of hello world reflects the traditional use of J – programs are entered into a J interpreter session, and the results of expressions are displayed. It's also possible to arrange for J scripts to be executed as standalone programs. Here's how this might look on a Unix system: #!/bin/jc
echo 'Hello, world!'
exit ''
(Note that current j implementations install either Historically, APL used Because ASCII does not include a division symbol per se, J uses % to represent division, as a visual approximation or reminder. (This illustrates something of the mnemonic character of J's tokens, and some of the quandaries imposed by the use of ASCII.) Defining a J function named
This is a test execution of the function:
Above, avg is defined using a train of three verbs ( Some examples of using
Rank is a crucial concept in J. Its significance in J is similar to the significance of Implementing quicksort, from the J Dictionary yields: sel=: adverb def 'u # ['
quicksort=: verb define
if. 1 >: #y do. y
else.
(quicksort y <sel e),(y =sel e),quicksort y >sel e=.y{~?#y
end.
)
The following is an implementation of quicksort demonstrating tacit programming. The latter involves composing functions together and not referring explicitly to any variables. J's support for forks and hooks dictates rules on how arguments applied to this function will be applied to its component functions. quicksort=: (($:@(<#[), (=#[), $:@(>#[)) ({~ ?@#)) ^: (1<#)
Sorting in J is usually accomplished using the built-in (primitive) verbs The following example demonstrates the usage of the self-reference verb 1:`($:@-&2+$:@<:)@.(>&2)
This recursion can also be accomplished by referring to the verb by name, although this is of course only possible if the verb is named: fibonacci=:1:`(fibonacci@-&2+fibonacci@<:)@.(>&2)
The following expression exhibits pi with n digits and demonstrates the extended precision abilities of J:
Verbs and ModifiersA program or routine - something that takes data as input and produces data as output - is called a verb. J has a rich set of predefined verbs, all of which work on multiple data types automatically: for example, the verb i. searches within arrays of any size to find matches: 3 1 4 1 5 9 i. 3 1 NB. find the index of the first occurrence of 3, and of 1
0 1
3 1 4 1 5 9 i: 3 1 NB. find the index of the last occurrence of 3, and of 1
0 3
User programs can be named and used wherever primitives are allowed. The power of J comes largely from its modifiers: symbols that take nouns and verbs as operands and apply the operands in a specified way. For example, the modifier / takes one operand, a verb to its left, and produces a verb that applies that verb between each item of its argument. That is, +/ is a verb, defined as 'apply + between the items of your argument' Thus, the sentence +/ 1 2 3 4 5
produces the effect of 1 + 2 + 3 + 4 + 5
+/ 1 2 3 4 5
15
J has roughly two dozen of these modifiers. All of them can apply to any verb, even a user-written verb, and users may write their own modifiers. While modifiers are powerful individually, allowing
some of the modifiers control the order in which components are executed, allowing modifiers to be combined in any order to produce the unlimited variety of operations needed for practical programming. Data types and structuresJ supports three simple types:
Of these, numeric has the most variants. One of J's numeric types is the bit. There are two bit values: 0, and 1. Also, bits can be formed into lists. For example, Further, J supports all the usual binary operations on these lists, such as and, or, exclusive or, rotate, shift, not, etc. For example, 1 0 0 1 0 0 1 0 +. 0 1 0 1 1 0 1 0 NB. or 1 1 0 1 1 0 1 0 3 |. 1 0 1 1 0 0 1 1 1 1 1 NB. rotate 1 0 0 1 1 1 1 1 1 0 1 J also supports higher order arrays of bits. They can be formed into two-dimensional, three-dimensional, etc. arrays. The above operations perform equally well on these arrays. Other numeric types include integer (e.g., 3, 42), floating point (3.14, 8.8e22), complex (0j1, 2.5j3e88), extended precision integer (12345678901234567890x), and (extended precision) rational fraction (1r2, 3r4). As with bits, these can be formed into lists or arbitrarily dimensioned arrays. As with bits, operations are performed on all numbers in an array. Lists of bits can be converted to integer using the J also supports the literal (character) type. Literals are enclosed in quotes, for example, Finally, there is a boxed data type. Typically, data is put in a box using the <1 0 0 1 0
+---------+
|1 0 0 1 0|
+---------+
The only collection type offered by J is the arbitrarily dimensioned array. Most algorithms can be expressed very concisely using operations on these arrays. J's arrays are homogeneously typed, for example the list J also supports sparse numeric arrays where non-zero values are stored with their indices. This is an efficient mechanism where relatively few values are non-zero. J also supports objects and classes,[14] but these are an artifact of the way things are named, and are not data types. Instead, boxed literals are used to refer to objects (and classes). J data has value semantics, but objects and classes need reference semantics.[citation needed] Another pseudo-type—associated with name, rather than value—is the memory mapped file. DebuggingJ has the usual facilities for stopping on error or at specified places within verbs. It also has a unique visual debugger, called Dissect, that gives a 2-D interactive display of the execution of a single J sentence. Because a single sentence of J performs as much computation as an entire subroutine in lower-level languages, the visual display is quite helpful. DocumentationJ's documentation includes a dictionary, with words in J identified as nouns, verbs, modifiers, and so on. Primary words are listed in the vocabulary, in which their respective parts of speech are indicated using markup. Note that verbs have two forms: monadic (arguments only on the right) and dyadic (arguments on the left and on the right). For example, in ' Control structuresJ provides control structures (details here) similar to other procedural languages. Prominent control words in each category include:
See also
References
External links
|