Programming in J with Words

Back to Topics in J


Introduction

This article describes a technique for programming in J.

Many J primitives are ASCII character combinations, such as .+ for the "or" function, >. for "max", etc. Although one can master the combinations with practice, as with any subject, some people find it handy to substitute "word synonyms". (This is easily done using standard function definition.)

   4 {. 1 10 2 20 3 30 >. 5
5 10 5 20

   take =. {.
   max =. >.
   4 take 1 10 2 20 3 30 max 5
5 10 5 20

Discussion

I like this list as a starting point. It is not suggested to substitute a word for each J primitive: for example, no words are defined for single-character primitives, for "combining" conjunctions such as @. , or for primitives consisting of a "keyword", such as i. (Not necessary nor advisable, in my opinion).
The terms follow the convention of beginning with a capital letter.
The terms are exactly those used in the J Dictionary to describe the primitives. Not surprisingly, these terms are similar or identical to corresponding APL primitives, taking due account of some semantic differences, and can therefore help people who know APL to learn J.
(Thanks to Chris Burke for many helpful suggestions for this list.)

Example

First, we introduce some definitions (synonyms):

Nand =: *:
Drop =: }.
Rotate =: |. NB. dyad

Consider the function "dbrl" which deletes redundant blanks from a vector. The function will produce the following result:

dbrl ' a  b   c '
a b c

Now consider the definition written in two different but equivalent ways:

dbrl =: 3 : 0
b =. y. = ' '
1 }. ((1,b) *: 1 |. 1,b)#' ', y.
)
dbrlw =: 3 : 0
b =. y. = ' '
1 Drop ((1,b) Nand 1 Rotate 1,b)#' ', y. )

A further discussion of this function with other examples can be found in the article Deleting Redundant Blanks.