Help / JforC / Culture Shock

From J Wiki
< Help(Redirected from Help/JforC/Culture Shock)
Jump to navigation Jump to search

>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help J for C Programmers

                                                                                                                            2.     Culture Shock

A C Programmer's First Thoughts On J


This doesn't look like a program.  Where did the program go?

You have gotten used to the pace and structure of C.  You can recognize a loop, you figure out what the loop is doing, you see what variables it sets.  The assignments and the loops are signposts that direct your analysis of code.

In J, every operator has a loop built in, so they don't jump out at you in the same way.  And because the syntax of J is so terse, and arrays use the same syntax as single variables, there is much less need for temporary variables.

Here's an example.  Figure out what the following code does:


int i, j, maxcol = 0;

float maxval = x[0][0];

for(i = 0;i<=xsize0;++i) {

  for(j = 0;j<=xsize1;++j) {

    if(x[i][j] > maxval) {

      maxval = x[i][j];

      maxcol = j;





Not too hard.  When the code finishes, maxval is the largest element in the array x, and maxcol is the column number it was in.  As it happens, all I wanted was the column number, but there was no way for you to know that.

The same code in J:

maxcol =. (i. >./) >./ x

With some practice, you will learn to read this code just as easily as you read the C.  You will recognize the / as an indicator of a loop that accumulates a result, and the i. as an indicator of a search.  The =. and =: tokens indicate assignment.


What happened to the if statement?

It's built into the >. primitive.  Just as most loops are hidden inside primitives, so are most conditionals.  The functions you write can also contain built-in conditionals.


What's the statement delimiter?

There isn't one.  Statements are exactly one line long.


I've looked at some J code.  Every other character is a period or a colon.  I've got spots before my eyes.  How can anybody read this stuff?

You'll get used to it.  J has a great many primitives, and it's important to keep the names short so that you can fit a lot of computation on one line; so the names are either single characters, like >, or a character with a period or colon appended (>. and >:).  The period/colon is just part of the name.  Single letters with period/colon, like i., are also used for primitives.  If you want to assign your own names to the primitives, you are allowed to, but pretty soon you'll want to go back to the shorter names to save space and typing.


Where are the declarations?  Doesn't J use arrays?  How do I know what the type of a variable is?

Oh yeah, J uses arrays.  Any variable can be an array.  As for what the type and dimensioning is: you assigned the variable, didn't you?  It contains whatever you put into it, a number, a string, an array, a structure...  J will remember.  If your program logic requires you to find out the current attributes of a variable, J can tell you.

Every J program, both primitives and programs that you write, can automatically be applied to arrays.  The carefully-thought-out way this is done is one of the prime strengths of J.  The statement

   x + y

means 'add x and y'; it will do that if x and y are single numbers, or if they are multidimensional arrays.  The looping needed to operate on the individual numbers is built into the language, not into your program.


I've looked at a line of J code and it's just a mess with no discernible syntax.

Something like

   lifodd =. ]`[@.(2&|@[)

perhaps, a program that returns the left operand if it is odd, otherwise the right operand? 

Patience.  Every character has a meaning, even the ` " [ ] { } characters which act individually, not as pairs.  Learn what they mean and all will become clear.


I ran across this line:  1 2 + 3.  What's with the extra number floating in space?

1 2 is a one-dimensional array of 2 numbers with the values 1 and 2.  Arrays come up so much in J that we use this shorthand to define one without requiring any more syntax than just the values of the elements of the array.


I've looked at a line of J code and I'm absolutely sure there are no variables in it at all.  How can that mean anything?

Oh, you've stumbled onto a tacit program, which describes its computation without referring to any variables, not even symbolic parameters.  The classic teaching example is

   mean =. +/ % #

which finds the mean of an array of numbers.  Tacit programs are considered an advanced topic in this book, and are covered in a section at the end.  They are the ultimate in concise expression; many J programmers favor them for that reason, but you can wait to learn about them until you've learned simple J.


In the examples all I see are lines that contain =: and =.,which you said are assignment statements.  How do I write a program?

Some of the assignment statements are programs.  In J, a name becomes a function name when you assign it a value that is a function.


Once I've written a program, how do I run it?  I don't see anything that looks like a function call.

In J, there is no special syntax for a function call.  Just as you perform the 'minus' function with - y or the 'subtract' function with x - y, you invoke a user-defined function by typing its name before or between its operands: x DifferenceSquared y or FindPrimeGreaterThan 1000.


How do I compile my program?

You don't.  J is interpreted.  As soon as you type your program in (or read it in from a file), it's ready to use.  Here's a starter program:

   h =. verb : '''Hello world.'''

Type that (note the triple quotes), then run it with

   h ''


The triple quotes are ugly.

Quit bellyaching.  Read the book and you'll learn how to avoid them.  Remember those 10 lines of C that were replaced by a quarter-line of J, and learn how to do that yourself.


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help J for C Programmers