Archives
 
 
 
  Special
 
 
 
  About Us
 
 
 

Newsletter
Free E-mail Newsletter from BYTE.com

 
    
           
Visit the home page Browse the four-year online archive Download platform-neutral CPU/FPU benchmarks Find information for advertisers, authors, vendors, subscribers Request free information on products written about or advertised in BYTE Submit a press release, or scan recent announcements Talk with BYTE's staff and readers about products and technologies

ArticlesThe Joy Of J


September 1995 / Core Technologies / The Joy Of J

One line of J can do the same work as hundreds of lines of Pascal or BASIC

Dick Pountain

If you are involved in mathematical programming then you need to know about the J programming language. Even if you perform less abstract tasks like analyzing financial data from a corporate RDBMS (relational database management system), you will find J interesting.

J is the modern successor to APL, a language that developed a cult following among some corporate IBM mainframe users in the 1960s as a rapid and powerful (but cryptic) data processing tool. APL suffered from its use of an unorthodox character set (that included Greek characters, among others), which didn't sit well on ASCII text displays and keyboards.

J is a truly new language by APL's author, Kenneth E . Iverson, and implemented by his son Eric and colleague Ronald Hui. It's available on a wide range of platforms including DOS, Windows, OS/2, Unix, and Macintosh. J is more than just an ASCII-fied APL, but it retains the same fundamental principles. Ironically, Windows and the Mac could now support APL characters, but J sticks to ASCII characters -- and is the better for it.

I've been using the Windows release of J version 2.05, which can be a powerful calculating engine for Visual Basic programs. The J system provides a DDE server which you can include in your VB (Visual Basic) programs, allowing you do the math in J while writing the user-interface and file handling parts of your application in VB. J also comes with its own Windows-based development system so you can write stand-alone J programs that employ the Windows interface on their own account, including DDE, OLE, ODBC (Open Database Connectivity), VBX (Visual Basic custom controls), and all the other trimmings.

J i s an interpreted language, though this fact usually has little impact on J's processing speed. The language's primitive functions are written in C and are highly optimized. They will often run faster than the obvious equivalents you might write yourself.

J employs a functional style in which expressions are evaluated from right to left. It does support dyadic operators (with both left and right arguments) such as + so that arithmetic looks quite familiar. There is only one data structure in J: the array. A number like 2 is treated as an array of rank 0, and text is an array of characters. You create new named objects simply by assigning them values (this includes programs). J allocates and frees memory automatically and invisibly, and the only limit on the size of an object is available memory.

The Language

The syntax of J is extremely simple and regular; all functions have the same priority; parentheses are the only way to alter execution order. J's components are named using terms taken from English grammar: Functions are called verbs, constants are called nouns, adverbs and conjunctions modify the action of verbs. In fact, J is an executable mathematical notation, and Iverson has written a series of math textbooks, up to and including vector calculus, using J as the descriptive notation.

The J language consists largely of 70 or so verbs. Although J has abandoned APL's hieroglyphics, its verbs still have cryptic two-character names like >. or #: . Here is a one-line program to compute Hellerman's distance-squared similarity measure for a matrix of any size :

dsqt=. +/"1@*:@(-"1/)

You could be forgiven for mistaking it for line-noise. To be fair, J doesn't have to look this scary; mean=. sum % count is a perfectly good J definition. The point is, someone fluent in J could hack dsqt from the keyboard during an interactive session, using J like a calculator to do statistical cluster analysis.

Math problems that would take hundreds of lines of Pascal or BASIC take one line of J. So, J is not a language for the faint of heart. Learning the syntax and semantics of all the primitives will take time, but the reward is that you can then do extraordinarily intricate array and matrix computations that would be difficult or impossible in, say, a spreadsheet.

Most of the verbs either generate arrays or perform operations on elements of arrays. You can combine primitive operations by using adverbs to form new operations. For example, i. 89 generates a list of the integers up to 89, and + is the humble sum operation. The adverb / -- called "insert" -- causes its left argument to be inserted between the elements of its right argument. So +/ x means sum the whole list x , and hence +/ i. 89 sums all the integers up to 89. Or again, the adverb "tie" is represented by ` . Its action is to combine several verbs into one (called a gerund). The gerund +`*`- when applied to 1 2 3 4 will calculate 1+2*3-4 . If you need to use such constructs more than once, you can give them a name, as in

ger=. +`*`-

where =. is just the assignment operator. To save names permanently, you store the text into script files, which when loaded execute as though typed from the keyboard.

Unlike APL, J supports conventional control structures like if...then and while . You could define factorial as:

factorial=. 3 : 0

a=. 1

while. y. > 1

do. a=. a*y.

y.=. y. - 1

end. a

)

(where y. refers to the verb's right argument), though a hardened J-bird would probably prefer the cryptic definition:

factorial=. 1:`(]*factorial@<:) @. *

Actually it's pointless to do either, as factorial is a built-in primitive verb, ! .

Another J innovation is the concept of a "locale," or private name space, so that mydef defined in locale A is different to mydef in locale B. Locales enable you to write modular applications while avoiding name clashes.

Finally, an important (if rather unaesthetic) feature of J is the "foreign conjunction" which is the way you do system dependent, nonmathematical things. The conjunction !: takes two numbers as left and right arguments to produce a verb which is a system call. For example, 1!:0 is the directory call, so 1!:0 `*.txt' will list all the text files in the current directory. This construct compounds J's unreadability, though a diligent programmer can always write a library of meaningful synonyms.

Windows and J

You can write J programs that fully exploit the Windows graphical interface (or the Mac, or OS/2, and so forth) by using verbs called wd commands (actually, wd is a friendlier synonym for 11!:0 ). For example,

wd `mb "Dick says" "Hello!" ;'

pops up a Windows message-box with title "Dick says" and content "Hello." J's interpreted nature doesn't mean that windowing operations are slow. As with VB, when a window is open it's mostly Windows GDI (Graphical Device Interface) code being executed.

J's Windows driver provides 10 control classes: button, edit, listbox, combobox, scrollbar, static text, isigraph, isipicture, isiole, and vbx. Isigraph is a graphics box, and J contains many graphics commands (e.g., gpolygon) to draw in it, while isiole is the graphical presentation for an embedded OLE object. The vbx control class allows you to add VBX controls into J programs. J can drive other windows applications via DDE links and OLE 1 linking and embedding, but it does not yet support OLE 2.

J's vedit verb let's you visually edit any parent window and its controls (i.e., by dragging with the mouse), and J's publishers have used it to good effect in writing a simple but effe ctive forms editor supplied with J. The editor lets you build an application by choosing controls from a menu, like a mini-VB.

Though J's Windows interface is powerful and well thought out, it's still easier to write complicated user interfaces using VB, and J lets you do just that. Including JDDE.FRM and JDDE.BAS in your VB project gives you a DDE link to a J server, along with an attendant API. For example, the VB routine jdo(s$) executes its string argument as an expression in J, while a variety of data exchange routines will retrieve values from J and format J-style arrays in VB-style arrays. You can even make an executable version of your VB application that includes a J run-time server.

Finally, J is a powerful tool for manipulating numeric data held as tables in a corporate relational database. A ddsql verb lets you execute SQL statements directly from J code.

The Way of J

J is not a programming language that everyone will take to, but it will prove interesting and useful to more than just those confined to mathematics departments. Programmers working in such business sectors as insurance, banking, derivatives trading, and planning need precisely the combination of ultrasophisticated math functionality, database connectivity, and a Windows interface that J so capably provides.


Where to Find

Strand Software, Inc.
Shorewood, MN
(612) 470-7345
fax: (612) 470-9202
amfaust@aol.com



Numeric Integration

illustration_link (6 Kbytes)

This J demo program is displaying some of its own built-in controls and some external VBXs, like the graph and the spinbox.


J's Surface Simplicity Conceals Complex Depths

screen_link (29 Kbytes)

You could use J like a calculator to do statistical cluster analysis


Dick Pountain is a BYTE contributing editor based in London, U.K. You can reach him on the Internet or BIX at dickp@bix.com .

Up to the Core Technologies section contentsGo to previous article: Endian IssuesGo to next article: Springtime at SunSearchSend a comment on this articleSubscribe to BYTE or BYTE on CD-ROM  
Flexible C++
Matthew Wilson
My approach to software engineering is far more pragmatic than it is theoretical--and no language better exemplifies this than C++.

more...

BYTE Digest

BYTE Digest editors every month analyze and evaluate the best articles from Information Week, EE Times, Dr. Dobb's Journal, Network Computing, Sys Admin, and dozens of other CMP publications—bringing you critical news and information about wireless communication, computer security, software development, embedded systems, and more!

Find out more

BYTE.com Store

BYTE CD-ROM
NOW, on one CD-ROM, you can instantly access more than 8 years of BYTE.
 
The Best of BYTE Volume 1: Programming Languages
The Best of BYTE
Volume 1: Programming Languages
In this issue of Best of BYTE, we bring together some of the leading programming language designers and implementors...

Copyright © 2005 CMP Media LLC, Privacy Policy, Your California Privacy rights, Terms of Service
Site comments: webmaster@byte.com
SDMG Web Sites: BYTE.com, C/C++ Users Journal, Dr. Dobb's Journal, MSDN Magazine, New Architect, SD Expo, SD Magazine, Sys Admin, The Perl Journal, UnixReview.com, Windows Developer Network