[Home]

Table of contents


Delving deeper

In the last two pages I have said that J allows the prgrammer to construct required functions easily by combining some basic building blocks. Naturally, you need to be familiar with the building blocks in order to take advantage of this.

In this page we shall learn about a few such functions. But before we start, let me point you to master resource for these building blocks: NuVoc.

Visit this site. It is as informative and indispensible as a dictionary, and about as appetizing.

The first thing that you should notice is that all the basic functions have names consisting of at most two characters. If it has two characters, then the second character is either a period or a colon. This introduces a natural grouping: a single character, the character followed by a period, and the character followed by a colon. Each resulting symbol has two possible interpretations: monad and dyad. So you get at most 6 different functions in a group. These 6 functions are usually related, so that it is easy to remember their names. The NuVoc lists them according to this natural grouping.

But trying to remember the basic functions by groups is as boring and inefficient as trying to build vocabulary by reading a dictionary. Instead, we shall group them here by usefulness and familiarity.

Familiar function, familiar symbol

Here are the basic functions that you expect to be present in any language, and J uses the same symbols as most other languages:

+, -, *, ^ (dyads) These all behave exactly as you'd expect, except that evaluation is always from right to left.

< , > (dyads) These are the familiar comparisons. They return 0 or 1. J does not have any separate boolean data type.

', ( and ) These are delimiters, not functions. They work as usual. Strings in J are always delimited by single quotes. If you need to have a single quote inside a string just repeat the single quote, e.g., to create the string "Don't do this." you need to write 'Don''t do this.'

Familiar functions, unfamiliar symbols

i., i: (monad) Sequences.

i., i: (dyad) Index of first occurence: list i. elt. Index of last occurence: list i: elt.

j. (monad and dyad) Imaginary. Complex.

p. (monad and dyad) Find roots: p. coeffs in increasing order Eval poly: coeffs p. where

% (dyad) This is division.

#. and #:(monad and dyad) From/To some base to/from decimal.

=, <:, >:, ~: (dyads) These are all conditions testing for equality, $\leq$, $\geq$ and $\neq.$ A common mistake is to use = for assignment.

=., =: (dyad) These are for assignments. The first is for local assignment, the second for global. Unless used inside a script file or function these behave similarly. But, if used inside a file, the effect of local assignment is confined within the file or function, while the global assignment has effect visible everywhere.

<., >. (monads and dyads) As monads, these are floor and ceiling functions, respectively. As dyads, these compute minimum and maximum of two numbers.

+., *. (dyads) , -. (monad) These are boolean operators, or, and, not.

^, ^., %: (monads) These are $e^x$ and $\log x$ and $\sqrt x$.

%. (monad and dyad) Matrix inversion and least squares solution.

i. (monad) Creates a lis 0 1 2 3 ... up to one less than the argument.

] (monad) identity funtion.

|: (monad) transpose. Actually more general than that.

NB. Comment. Not a function.

-. (dyad) Set minus

! (monad and dyad) Factorial when monad, choose when dyad. You write ! 3 to mean $3!$ and 3 ! 4 to mean $^4C_3.$

?, ?. (monad and dyad) ? 10 generates a random number from $\{0,1,...,9\}.$ ? 0 generates a random number from $Unif(0,1).$ 3 ? 10 generates an SRSWOR of size 3 from $\{0,1,...,9\}.$ The dotted version is similar, but uses fixed seed. Produces different results for 64 bit and 32 bit machines.

|(monad and dyad) Absolute value as monad. Remainder as dyad, e.g., 3 | 4 returns 1.

Less familiar functions

~. (monad) unique values

~: (monad) positions of first occurences of unique values

q: (monad) prime factors.

q: (dyad) prime factor exponents.

Strange functions

e.,