Another programming language for Sugar

The news today is that J Software has put its J programming language under GPL3, meaning that it is now Free Software (Open Source, too, if you like) available for use with Sugar. J is Turing Award winner Ken Iverson’s final version of APL, which he was working on right up to his death a few years ago.

I care about this deeply, because my favorite programming language is APL, and my favorite version of APL is J. My father, a mathematician and actuary for an insurance company, go me started on APL. It took in part because I trained to be a mathematician before I went off into the Peace Corps and the monastery, and wound up here. But why would you care? To explain that, I must tell you a bit of a story, and then show you briefly.

Python and Smalltalk, built into Sugar, are also excellent languages, as is Logo, available as output from Turtle Art to use with UCBLogo. As long as we are not talking about COBOL or FORTRAN, or maybe C++, there is no need for an rwar (“religious” flame war) about languages among us. (Alan Kay of Smalltalk fame coined the term Object-Oriented Programming, and said in 1997, “C++ was not what I had in mind.”)

I have been working on a set of Turtle Art tutorials, as has Tony Forster. He has blogged about using Python from inside Turtle Art, and I plan to do some lessons on how to transition from Turtle Art to Python and other languages, including Logo, Smalltalk, FORTH, and now J. Waveplace and Squeakland have created excellent tutorials for Etoys/Squeak/Smalltalk. There is also a vast literature on each of these languages. For Logo and Smalltalk there is material on using them with children created over a period of 50 or 40 years respectively. Research has shown that these and several other programming languages can easily be taught to third graders.

All of that is reasonably well known. It is less well known that Ken Iverson, the inventor of APL, got IBM to loan an IBM 360 and a number of Selectric printing terminals to a school, where he successfully taught first-grade arithmetic using APL. He wrote a book for APL Press on Arithmetic, and others on Algebra and Elementary Analysis (Calculus). There are other books on math in APL and J listed on the J Software bibliography page.

I cannot do this subject justice in one blog post. Here is a start, though, to give you a bit of the flavor of J, and show why it is good for early arithmetic and other math. I will be back with more.

   NB. The NB. primitive marks comments, which are not executed.
   NB. [APL ⍝] The interpreter indents three spaces to accept
   NB. user input. Output starts at the left margin.
   1  NB. The result of any input line is printed as output.
   1+2  NB. J has standard arithmetic functions.
   1-2  NB. The negative sign is _. [APL ¯]
   1%2  NB. No division sign in ASCII [APL ÷]
   1 2  NB. Arrays (nouns) are first-class data objects in J
1 2
   a =. 1 2  NB. Assignment to a variable (pronoun).
             NB. Pronounced 'gets'. No value is displayed.
   a + 2 3   NB. Functions (verbs) apply to arrays without loops.
3 5
   a + 4 NB. Adding one item to each item of an array.
5 6
   #a  NB. 'Count' of items in an array. [APL ρ]
   i. 10 NB. First 10 numbers [APL ι] 'index'
0 1 2 3 4 5 6 7 8 9
   x*x=. i. 10  NB. Squares [APL ×]
0 1 4 9 16 25 36 49 64 81
   x*/x  NB. Multiplication 'table' [APL ∘.×]
0 0  0  0  0  0  0  0  0  0
0 1  2  3  4  5  6  7  8  9
0 2  4  6  8 10 12 14 16 18
0 3  6  9 12 15 18 21 24 27
0 4  8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81
    a =. 3 9 7 NB. Assignment
    +/a NB. 'Sum' is 'plus reduce'.
    (+/a)%(#a) NB. 'Mean' is 'sum divided by count'
    (+/ % #) a NB. (f g h) a is evaluated as (f a) g (h a)
    a * a
 9 81 49
    +/ a * a
    a +/. * a NB. Vector inner product, 'sum dot times'
    3 >. 2 NB. Maximum
    3 <. 2 NB. Minimum
    >. / a NB. 'Max reduce'
    <. / a NB. 'Min reduce'
    (>. / a) - <. / a
    range =. (>./) - (<./) NB. Function assignment
    range a

About mokurai

Generalist; End poverty at a profit for all
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

11 Responses to Another programming language for Sugar

  1. I followed along up to the multiplication tabel. What happened to 9*9 since 8 seems wrong to me. And I don’t understand the meaning of the notation itself.

    I wonder how to pronounce these things so that they make sense directly in words. Indeed, I’d be interested to see a convenient way to see any unfamiliar notation in a worded form.

    How do you make a block comment preceding a complex or multi-line construction? Literate programming is easier with good commenting conventions. In Python we used two tiny programs to convert Humane Python programs, where any line beginning in column one or two is commentary, into compilable form, and back. This makes keeping the commentary up to date easier using common text editors, since there is no need to reformat manually after adding or deleting say six words.

    What other tools help novices to understand large and or complex chunks of J code? Even one’s own code can be obscure a month later or sometimes even a day.

    • mokurai says:

      Thanks, Richard. I corrected the ‘8’ to ’81’, and added pronunciations to the comments. The example begins with a block comment. It would not be difficult to write a pretty-printer in J, or to use the names in the J dictionary

      as the basis for a translator to worded form.

      One of the ways to keep J code readable is to write short modules, remembering that 10 lines of J might be the equivalent of 100 lines of Python or C++. And yes, you can write object-oriented J.

      Please let us know if you have more questions. I will post about ways to learn J, and ways to use it to teach programming, math, and other subjects, as I get a chance to write those tutorials and post them on the Sugar Labs Wiki.

  2. Pingback: Arithmetic with fractions | Replacing Textbooks

  3. Patrick PAUMARD says:

    I think that the 8 is for 81 an error with ctrl-C and ctrl-V
    I Hope a mixed language with the power of J and the the APL’s symbols
    I don’t like in J: i.4 I like iota 4 in APL
    i think that there are many step between the languages
    first May be COBOL Basic …
    third APL 2 (with Nars)
    and the last “J”

  4. mokurai says:

    ‘8’ error fixed.

    The source code for J is readily available. It would be almost trivial to modify it to use APL symbols. It would also be easy to write a translator in J between the ASCII and Unicode APL forms.

    The progression we will be using in Sugar education software starts with Turtle Art. TA can write out linear Logo programs for any tree-structured or forest-structured TA program. There are program blocks in TA for calling Python expressions or programs. Etoys has a Turtle Graphics module in Smalltalk. TA has stack primitives which one can use for computer science topics or as an introduction to FORTH and RPN calculator concepts. (Open Firmware on OLPC XOs is in FORTH.) I see no reason to bother children with more primitive languages. As we say in the community

    BASIC isn’t.

    APL, the un-COBOL.

  5. Now that I’m learning more of J, should I look elsewhere for a “J for Dummies” tutorial or is this intended to serve to introduce the language to those with some vague knowledge of computer languages? Presuming, for the moment, the latter, I have some questions of detail:

    Are there parts to “=.” which naturally combine to mean ‘gets’?

    Would it be correct to claim: “i.” is ‘index’, which when applied to an integer results in an array of that many numbers chosen sequentially from “0”, ‘zero’?

    It seems that “x*x=.” is a function where “x” stands for ‘argument’, and “*” for ‘times’, but what is the meaning of “=”? My best guess is that “.” means ‘applied to’ and in consequence “=.” without a simple variable name before it means roughly ‘results in’.

    What are the parts of “x*/x” and why doesn’t it need the “=.”?

    • mokurai says:

      Good questions. I will put them into my tutorial.

      I have introduced only a tiny part of J in this blog post, just enough, as I said, to give a bit of the flavor and to demonstrate that it would be useful in teaching arithmetic, with none of the apparatus of print statements and loops that other languages require.

      For tutorials, try

      for materials beginning with the Primer and online Lab demonstrations, and continuing the the full reference manual, The Dictionary. This page also has links to various books about J at various levels.

      ‘=.’ is assignment to a local variable. We don’t teach global assignment to beginners, so that they don’t form the bad habit of using it without thinking whether they need to.

      You are correct about ‘i.’.

      Dots are inherently part of the names of many J primitives (built-ins). ‘= .’ or ‘i .’ would be meaningless.

      In the line ‘x * x =. i. 10’, ‘x’ is a variable name, ‘*’ is the ‘times’ function, and ‘=.’ is assignment. J is executed from right to left, like APL, so the assignment at the right is carried out first. Then the variable on both sides of the multiplication has a value, and we can carry it out. Function definition has the form of function assignment: ‘name =. anonymous_function’. Thus, for example,

      times =. *
      2 times 3

      In ‘x */ x’, ‘x’ was a variable that we had previously given a value, and ‘*/’ (times table) is the result of applying the table operator (higher-order function) to the function ‘*’. The function ‘*’ applies to two arguments. ‘*/’ applied to just one argument (called times reduce) gives us products of elements of a vector, the columns of matrices (rows optional), and so on in higher dimensions. Thus,

      ‘*/ 1 + i. 7’

      is equivalent to

      ‘1 * 2 * 3 * 4 * 5 * 6 * 7’

      which is in turn equivalent to ‘! 1 + i. 7’, or 5040.

      Almost all J primitives have two names for the two different uses with either one or two arguments.

  6. Pingback: Wordless proofs | Replacing Textbooks

  7. Pingback: Belated apology | Replacing Textbooks

  8. Paul Jackson says:

    Nice work. I have two comments.

    The inner product example has a missing blank.
    a +/.* a NB. Vector inner product
    should be
    a +/ .* a

    Introducing embedded assignment in introductory material leads to confusion, and is not required to use J. As written, it also introduces =. before it is explained.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s