Contents
- How to add a FAQ answer?
- How to add a FAQ?
- How do I define the rank of a user-defined verb?
- How do I launch a browser page from J?
- How do I run code in the clipboard?
- How does J manage memory?
- What should I know about J licenses?
- How to do lexical closure in J?
- How to convert between numbers and character representations of numbers
- How to convert negative numbers between J and other applications
- Why do some non-ascii characters misbehave under (i.) and ($)?
- How do I save my workspace in J?
Use short page name here, e.g. Do Something. In the main heading use full question, e.g. How to do something?
How to add a FAQ answer?
J Wiki has a number of FAQ pages as listed in Guides. A FAQ page is an incidentally ordered list of FAQ entries in separate subpages. FAQ entries do not display authorship, while editing history is preserved.
Eligibility
Make sure that the question is of general recurrent interest, otherwise consider one of J Forums.
Make sure that the scope is small enough, otherwise consider an essay or a guide, etc.
- See if such question is already addressed.
- See if there is a similar entry that can be extended instead.
Procedure
Select an appropriate FAQ in Guides
- Browse a few entries to get the feel of style conventions
Use the Add Entry with a short name for the page in the format: Do Something
- In the editor study the format of the template
Fill in the question heading in the format: How to do something?
- Fill in the rest of the template following the suggested format
See Also
Contributed by OlegKobchenko
How to add a FAQ?
What to add?
Compile a lists of possible FAQs on this page. If you're not sure your question is a "FAQ", or whether it's appropriate for this list, or don't yet have a well presented response the the FAQ, just leave it on this page. You can do some research & come back to it later, or solicit help from other users (possibly on the Forums).
If you're sure you have a FAQ, it deserves an answer, and you have one, please add both the FAQ and the answer to the appropriate FAQ page (listed below).
Please try to phrase the FAQs and answers such that they're easy to search for when the question arises. For example, how do we phrase the question:
Why doesn't +:^:3 2 work?
when nothing in the J code is a given? When a newbie encounters this class of problem for the first time, what words would he search for?
Also (when possible), do some research. If the FAQ has been answered or provided for in the wiki, the forums, the standard library, or the user library, please provide a link, with attribution and a description.
Newbie FAQs
Why doesn't +:^:3 2 work?
$&> ,<'hello' is 5 but $ > ,<'hello is 1 5. Why?
Why doesn't (;: 'this is j')-:'this';'is';'j' hold?
General FAQs
Why does ''-:i.0 hold?
What's up with 'boxed' -: datatype >0$a:?
One way to look at it is: it's impossible to know the datatype of the contents boxes of that aren't there. Since boxed arrays can front any data type (and even multiple data types, i.e. when used to junction heterogeneous data), this answer is appropriate. What should be the result of datatype > 0 $ 'text';1bsome 1bnumbers;s:' symbols'?
- Arithmetic identity questions. Why do the following sentences hold?
Why are 0=0%0 and _=1%0? (i.e. explain division by zero):
Answered in the paper by E. E. McDonnell,
Why is 1=0^0 ?
Discussed on the college math 0^0 thread on the Forums, and may be required if 0=0%0.
- How can I configure or change those results?
You cannot change the result of a J primitive. You may, however, write a cover function
div =: 4 : 0 z =. x % y q =. _. _ __ {~ *x y0 =. ($z) $ y = 0 z =. y0 } z ,: q ) 10 100 1 _1 0 div 2 _10 0 0 0 5 _10 _ __ _.
Explain execution of a verb on an empty frame e.&0@:$ with a cell of fills.
See Also
How do I define the rank of a user-defined verb?
If I write my own verb and want to specify a default rank, how do I do this?
Simply specify the rank in the definition, e.g. for an explicit function:
myVecVerb=: 3 : 0"1 NB. Apply to rank 1 objects (+/y)-<./y NB. Sum less smallest in row ) (i.3 3);myVecVerb i. 3 3 +-----+------+ |0 1 2|3 9 15| |3 4 5| | |6 7 8| | +-----+------+ myMatVerb=: 3 : 0"2 NB. Apply to rank 2 objects (+/y)-<./y NB. Sum less smallest in column. ) (i.3 3);myMatVerb i. 3 3 +-----+-------+ |0 1 2|9 11 13| |3 4 5| | |6 7 8| | +-----+-------+
Specify for a tacit definition in much the same way:
myVecVerb=: (+/-<./)"1 myMatVerb=: (+/-<./)"2
More info
See Also
General introduction to the concept of ''rank'' in J
"Studio" introduction to ''rank'' with examples
Explanation of how negative values of rank work
How to determine the rank of verb combinations
How do I launch a browser page from J?
load '~system/extras/util/browser.ijs' launch_jbrowser_ 'http://www.jsoftware.com' launch_jbrowser_ 'file:///C:/dir/subdir/localpage.htm'
The browser used can be configured from menu Edit|Configure|External Programs|Web Browser. Check this if there are any problems launching the browser.
From a post by Ric Sherlock to the J Programming Forum on 2008-05-31.
How do I run code in the clipboard?
You might want this to run code from an email or other external source.
The easiest way is to copy the code to the clipboard, then in J, run the clipboard with:
0!:101 wdclipread''
You can define a function key to execute this expression. To do so, go to menu Edit|Configure|Fkeys, then Add, select a function key, say F8, then enter:
Output: quiet
Label: Run clipboard
Code: 0!:101 wdclipread''
and OK to close config. Then press F8 with the code in the clipboard.
This works for most emails. For more complex examples, you may need to create a new script, paste the email into the script and edit it before it can be run.
How does J manage memory?
How J's memory management works has been described on the forum on numerous occasions. Reading through the list of forum posts below (especially the last two) should give the reader a pretty good understanding:
What should I know about J licenses?
In particular,
- Is J open source?
- Where is the source code for J?
- Is J under the GPL?
- What license is J's source under?
Please note this Wiki page is user-written; for J Software's official policy on source, see http://www.jsoftware.com/source.htm .
The J interpreter is not open source. J Software, which owns and develops the J language, is a commercial venture. Part of J Software's revenue stream is selling its source code. Often, this is to companies with a large codebase or other stake in J, and owning the source provides them the confidence that they may continue their J-based business, should J Software (or their relationship with J Software) disappear.
That said: while the J interpreter is not open source, the Dictionary of J is readable by anyone. The Dictionary is the abstract, Platonically ideal, J. Any interpreter which implements its specification is as valid a "J" as the one provided by J Software (hence the title of the paper "An Implementation of J"). What remains is for an open source initiative to turn the English of the Dictionary into compilable code.
While no such intiative yet exists, OlegKobchenko and AndrewNikitin have started an effort at http://openj.sourceforge.net/. Further, prior to 1995, J's source was available. As of this writing in 2006, you can still find the source code to early versions of J in old FTP archives. While the language has changed significantly since then, the source might still be useful as seed code, or at least instructive in style.
How to do lexical closure in J?
If you want to know,
- Does J have lexical closure?
How do you solve Paul Graham's Accumulator Generator challenge in J?
please see Guides/Lexical Closure.
Contents
How to convert between numbers and character representations of numbers
Sometimes we have the character representation of a number but we want to work with number represented whereas, conversely, sometimes we wish to convert a number to characters. Representing numbers in bases other than 10 can be done using numeric or character vectors; see J Phrases for Changing Data Types
Number to Character Using Format in Newer and Older Form
Use one of the variants of the verb Format (8!:0) to convert from a number to its character equivalent:
fmt=: 8!:0 fmt 1 _2 3.14 4e_3 +-+--+----+-----+ |1|-2|3.14|0.004| +-+--+----+-----+
This boxes the results. However,
fmt2=: 8!:2 fmt2 1 22 333 4444 1223334444
does not.
Padding with Leading Zeros
The foreign Format (8!:0) takes an optional left argument of a format string - see the details here. One common use of a format string is to pad numbers with leading zeros, e.g.
'r<0>4.0' 8!:0 i.5 +----+----+----+----+----+ |0000|0001|0002|0003|0004| +----+----+----+----+----+
A general function like this
lead0s=: 13 : '(]`> @. (1=[:#,))(''r<0>'',(":x),''.0'') 8!:0 y'may also be helpful, e.g.
3 lead0s i.5 +---+---+---+---+---+ |000|001|002|003|004| +---+---+---+---+---+
Oleg Kobchenko recommends using take with fill {.!.'0' as the best generic method for leading zero padding:
_3{.!.'0' &.> (8!:0) i.5
+---+---+---+---+---+
|000|001|002|003|004|
+---+---+---+---+---+Or another yet more generic one:
zeropad=: (-@[ {. [: ": 10&^@[ + ])"0
5 zeropad 123 1234
00123
01234
2 5 zeropad&.> 123 1234
+--+-----+
|23|01234|
+--+-----+None of these methods account for negative numbers but that can be done as a separate step if required, perhaps using and adverb like this:
leadneg=: 1 : '(u |y),~''-''#~_1=*y' 3&zeropad leadneg"0 -i.3 000 -001 -002 5&lead0s leadneg"0 &.> -i.3 +-----+------+------+ |00000|-00001|-00002| +-----+------+------+
Foreign Format (8!:n)
From the Dictionary definition of the variants of this verb:
[x] 8!:0 y Format. Format array y according to format phrases x . Each formatted number is in a box. [x] 8!:1 y Format1. As in x 8!:0 y , but y has rank at most 2 and each formatted column is in a box. [x] 8!:2 y Format2. As in x 8!:0 y , but y has rank at most 2 and the result is a literal table.
So,
fmt1=: 8!:1 fmt1 i.2 3 +-+-+-+ |0|1|2| |3|4|5| +-+-+-+ fmt2 i.2 3 012 345
(Older) Number to Character (":)
For older versions of J in which 8!:n is not available, one can use the older, primitive version of Format (":) :
": 1 22 33.3 NB. Monad 1 22 33.3 'Numbers: ',": 1 22 33.3 NB. Show that result is character Numbers: 1 22 33.3
An optional left argument specifies a formatting string using complex numbers where the real part determines the length of the formatted number and the imaginary part determines the number of digits after the decimal point:
'Formatted numbers: ',6j1": 1 22 33.3 Formatted numbers: 1.0 22.0 33.3
Format Numbers as Character, Specifying Number of Digits to Display (RjI ":)
The primitive Format also takes a complex number as the optional left argument to specify total length of the resulting string (real part) and number of digits after the decimal point (imaginary part). For example, the value of the mathematical constant "e" shown to the limits of precision (specified by 9!:10):
^1 2.7182818
To show more digits as a character vector:
19j17 ": ^1 2.71828182845904510
Asking for more precision than we really have gives spurious trailing zeroes:
29j27 ": ^1 2.718281828459045100000000000
Allowing for too few digits before (and including) the decimal point:
16j15 ": ^1 ****************
Character to Number (".)
Use the verb Numbers (".) to convert from a character string to equivalent number. For example,
". '1 2 35' 1 2 35
To see that the result is numeric:
100+". '1 2 35' 101 102 135
The dyadic form of this verb may be faster and it allows a default value to be substituted for any string not convertible to a valid number:
_99 ".&>'123';'bad';'456';'nutherbad';'789' NB. Substitute "_99" for non-numbers
123 _99 456 _99 789
$nn=. '0123456789 '
13
many=. nn{~?1e7$#nn
6!:2 '".many' NB. Monad
2.2764244
6!:2 '_.".many' NB. Dyad using "indeterminate" substitution...
1.3662863
(_.".many)-:".many
1Converting numbers from an Excel spreadsheet has its own dangers; see caveats about numbers from Excel.
Note that the monadic form of "., called "Do", is a general verb for running a J phrase represented as a character vector, i.e.
". '1+2'
3
]randadd=. (":3?10),' + ',":3?100
0 6 9 + 48 49 29
". randadd
48 55 38
NB. Random equations:
eqn,' = ',": ". eqn=. (":?10),((]{~[:?#)'+-*%'),":?100
4%58 = 0.068965517
eqn,' = ',": ". eqn=. (":?10),((]{~[:?#)'+-*%'),":?100
9+30 = 39
eqn,' = ',": ". eqn=. (":?10),((]{~[:?#)'+-*%'),":?100
5*42 = 210This is why something like this
". '1 -2 -3' 2
might be puzzling until you understand that it is evaluating the J expression to perform successive subtractions, equivalent to
-/1 2 3 2
whereas the following may have been intended:
". '1 _2 _3' 1 _2 _3
See also the following "How to convert negative numbers..."] for this specific problem.
How to convert negative numbers between J and other applications
Negative numbers in J are preceded by the underscore character "_" whereas most applications use the dash "-" for this purpose. So, "_2" in J would be represented as "-2" in, say, Excel. A commonly-asked question is how to convert from one representation to the other.
Negative Representations to J Numbers
Typically, we use the dyadic verb Numbers (".) to convert from, e.g. -2 to _2, i.e.
_. ". &> '-2';'1e-2' _2 0.01
The left argument "_." is a number (Indeterminate in this case) to substitute for any non-numeric string. Indeterminate seems appropriate to use for a value we cannot figure out, but any other numeric value can be used. For example,
12345 ". &> '-3';'nonnumeric';'3e-3';'oops';'-3e-3' _3 12345 0.003 12345 _0.003
or
_99.9 ". &> '-3';'nonnumeric';'3e-3';'oops';'-3e-3' _3 _99.9 0.003 _99.9 _0.003
From J Number to Character Representation Suitable for Another Application: Underscore to Minus
Typically, we use the verb Format (8!:0) to convert from J to a more standard representation:
fmt=: 8!:0 fmt 1 _2 3.14 4e_3 +-+--+----+-----+ |1|-2|3.14|0.004| +-+--+----+-----+
Notice that fmt is smart enough to box its results; this is necessary because it must convert numbers into character strings.
The older version of Format (":) doesn't do as much but works with older versions of J; see the preceding section.
If you use this older version, you have to handle the negative sign replacement separately. Something like this would work for the simplest case:
cm=: ('_',a.)&i. { ('-',a.)"_
cm ": -12.34
-12.34
'A negative number: ','.',~cm ": -12.34
A negative number: -12.34.
cm ": -i.5
0 -1 -2 -3 -4
Why do some non-ascii characters misbehave under (i.) and ($)?
If I paste a non-ascii character (eg an accented vowel or an APL primitive) into a string, it behaves like several characters.
Thus: $'abc⌹e' returns the value: 7, not 5.
Further puzzling behaviour:
$z=: 'abc⌹e' 7 z i. 'ce' 2 6 z i. '⌹' 3 4 5 3 5 $z abc� �eabc ⌹ea
What must I do to make 'abc⌹e' behave like a string of 5 characters, with '⌹' behaving like a single character occupying position 3?
The answer involves the u. primitive, Unicode, utf-8 and converting to so-called "wide characters" (wchar).
See: Guides/UnicodeGettingStarted for an extremely simple explanation.
How do I save my workspace in J?
Many newcomers to J have previous experience with APL. Two common questions are:
Q. How do I save my workspace in J?
A. You cannot, instead J uses plain text files, known as scripts. However, if you want to store variables you have created to a file, you can use the WS code explained at File J Variables.
Q. My APL IDE has feature X, can I have this in J?
A. Each feature has to be examined on its own merits. It may be appropriate for APL, but not for J.
The key difference between development in APL and J is the following:
- The APL IDE is designed to support application development where source is stored in an internal binary format, that is inaccessible other than through the IDE.
- The J IDE is designed to support application development where source is stored in plain text files, like other programming languages.
This difference makes for very different IDEs.
For example, J has a Directory Match facility that compares scripts in one directory with another, useful for comparing different versions of an application's source. By default, the comparison uses J's standard compare utility, but you can also invoke an external diff/merge program, and this may be better for large scripts such as a target script. When J compare is insufficient, use a professionally written diff/merge program.
The corresponding APL IDE facility is Workspace Compare, but this has to be written entirely in APL, since there is no way an external diff/merge can examine two files in APL internal binary format to find out which functions changed.
In general, J's IDE is designed to accommodate, and be supplemented by, normal tools for managing source code, such as diff/merge, source code managers, search and replace utilities, other editors like Emacs, etc. In contrast, APL's IDE is designed to do everything, since normal source code tools will not work.
Irrespective of the merits of these two approaches, if you want to work with J, you need to become familiar with source in plain text files. An APL-style workspace is largely inappropriate to J, since not least, it would prevent the normal use of the J IDE and of other external source code tools.
See Also
