APL2JPhraseBook

From J Wiki
Jump to navigation Jump to search

APL to J Phrasebook

As an APLer learning J, I am constantly having to recall which J primitive to use for something I know perfectly well how to do in APL. They say that trying to transfer skill in APL is unhelpful for a J learner. You should unlearn APL and begin afresh. APL can certainly be misleading as a guide to J. It may actually hold you back. Thinking of Rank (") as an extended {each} (¨), for example. But what I usually need is just a memory jog. Other J learners report they have found File:J602 RefCard color letter current.pdf useful for this purpose.

No attempt is made here to translate APL into J. We simply show comparable J phrases -- and "comparable" should be interpreted liberally. A basic knowledge of J is needed to use this phrasebook (thesaurus) effectively. It's not meant to help you translate from APL.

Assume: ⎕IO=0 for all APL expressions

!

See: {factorial}

?

See: {roll} {deal}

a[i]

i { a                 NB. a[i]
a=: n i } a           NB. a[i]←n
a=: (201) 4 } i.6     NB. a←⍳6 ⋄ a[4]←201

NB: (201) not: 201 to prevent J seeing 201 4 as a single array.

⍺ {alpha} ⍵ {omega}

x ; y                 NB. (⍺ ⍵)

x. ; y.               NB. In legacy J code only.

Before J602 (& later) will recognise x. and y. you need to set Parameters in J Preferences.

∧ {and} ⍲ {nand} ∨ {or} ⍱ {nor}

*.           NB. AND  ∧
*:           NB. NAND ⍲
+.           NB. OR   ∨
+:           NB. NOR  ⍱

\ {backslash} ⍀ {backslashbar}

+/\a              NB. +⍀a
+/\"1 a           NB. +\a
1 0 1 #inv 'ab'   NB. expand  1 0 1\'ab'

→ {branch}

goto_xyz.    NB. →xyz
...
label_xyz.   NB. xyz:
return.      NB. →0

⌈ {ceiling} ⌊ {floor}

>.           NB. max / ceiling ⌈
<.           NB. min / floor   ⌊

○ {circle}

   o. 2      NB. 2*pi
 1 o. A      NB. sin
_1 o. A      NB. arcsin
 2 o. A      NB. cos
 3 o. A      NB. tan
 5 o. A      NB. sinh
 6 o. A      NB. cosh
 7 o. A      NB. tanh

, {comma} ⍪ {commabar}

   a=: 2 3 $'abcdef'
   b=: 2 3 $'uvwxyz'
   ,a        NB. (Ravel)      APL: ,a   ⍝ravel
abcdef
   ;a        NB. (Raze)
abcdef
   a,b       NB. (Append)     APL: a⍪b  ⍝catenate
abc
def
uvw
xyz
   a,.b      NB. (Stitch)     APL: a,b
abcuvw
defxyz
   $a,:b     NB. (Laminate)   APL: a,[2.5]b
2 2 3

NB: the differences between , ; ,. ,: (try with 2D and 3D arrays).
The APL parallels are not exact.

⍨ {commute}

a -~ b       NB. a-⍨b   ⍝equivalent to: b-a

? {deal}

See: {roll} {deal}

⊥ {decode} ⊤ {encode}

   b #. a       NB. (Base)     b⊥a   ⍝decode
   2 #. 1 0 1
5
   2 2 2 #. 1 0 1
5
   b #: a       NB. (Antibase) b⊤a   ⍝encode
   #: n         NB. convert scalar n to binary vec
   24 60 60 #: (24*60*60)-1
23 59 59

∇ {del}

foo=: verb define    NB. ∇z←x foo y ... ∇
  NB. code for monadic case
:
  NB. code for dyadic case
)

foo=: monad define   NB. ∇z←foo y ... ∇
...
)

foo=: dyad define
...
)

See also: ⎕FX

⋄ {diamond}

x=:3 [ y=:4   NB. x←3 ⋄ y←4

WARNING: whereas x←3 ⋄ y←4 is two statements, executed in turn, x=:3 [ y=:4 is one sentence, evaluated right-to-left.
C/f {lev/left} in some APLs.

⊃ {disclose}

>a          NB. (Open) ⊃a

÷ {divide} {reciprocal}

a % b       NB. a÷b
% b         NB. (Reciprocal) ÷b
-: a        NB. a÷2

⌹ {domino}

a %. b      NB. (Matrix Divide) a⌹b
%. b        NB. (Matrix Inverse) ⌹b

. {dot} +.× {plus-dot-times}

x +/ . * y  NB. (Inner product)   x+.×y

¨ {each}

a foo"0 b   NB. (Rank) a foo¨ b
foo each b  NB. b is a boxed list

WARNING: despite appearances, Rank is a fundamentally different concept to: ¨ {each}.

Verb: each_z_ is defined (in: stdlib.ijs) as: &.>

⊂ {enclose}

<a          NB. (Box) ⊂a

∊ {epsilon}

x e. y      NB. x∊y
;<@,S:0 y   NB. (Raze/Enlist) ∊y
; y         NB. often suffices

⍎ {execute}

". expr     NB. ⍎expr

! {factorial}

! a         NB. (Factorial) !a
b ! a       NB. (Out Of)    b!a  ⍝combinations

⍷ {find}

x E. y      NB. x⍷y

⍕ {format}

": a        NB. ⍕a

≥ {geq} > {greater} ≤ {leq} < {less}

a >: b      NB. a≥b
a > b       NB. a>b
a <: b      NB. a≤b
a < b       NB. a<b

← {gets}

a=: expr    NB. a←expr
a=. expr    NB. a←expr ⍝a is created as a "local" inside an explicit definition

¯ {hi-minus}

_1            NB. ¯1
__            NB. negative-infinity
10 _11 12     NB. 10 ¯11 12
10 -11        NB. 10-11 (c/f subtraction)
   10 -11 12  NB. treated the same in both J and APL, to yield…
_1 _2

⍳ {iota}

   i.6      NB. ⍳6
0 1 2 3 4 5
   i.2 3    NB. 2 3⍴⍳6
0 1 2
3 4 5
   i:6
_6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6
   z=: 0 10 50 30 40 20
   z i. 30      NB. z⍳30
3

∘. {jot}{dot}

a foo/ b    NB. a ∘.foo b

⍝ {lamp}

NB. ⍝this is a comment

⊣ {lev/left}, ⊢ {dex/right}

a [ b       NB. a⊣b  ⍝lev/left  -returns: a
a ] b       NB. a⊢b  ⍝dex/right -returns: b

≡ {level}

See: {match} {level}

⍟ {logarithm}

a ^. b      NB. a⍟b
^. b        NB. ⍟b

≡ {match} {level}

x -: y      NB. x≡y
L. a        NB. ≡a

∣ {modulus}

|a          NB. ∣a
b | a       NB. b∣a

~ {not}

-. b        NB. ~b

See also: {tilde}

≠ {notequal}

a ~: b      NB. a≠b

⌽ {phi} ⊖ {theta}

|. a        NB. ⌽a   (a is 1-D)
|. a        NB. ⊖a   (a is 2-D)
b |. a      NB. b⌽a  (a is 1-D)
b |. a      NB. b⊖a  (a is 2-D)

To rotate a 2-D array along the last dimension, you need: Rank.
To understand why, read carefully: Rank

|."1 a        NB. ⌽a   (a is 2-D)
b |."1 a      NB. b⌽a  (a is 2-D)

* {power}

^ n         NB. *n ⍝exponential
a ^ n       NB. APL: a*n
*: a        NB. APL: a*2    ⍝squared
%: a        NB. APL: a*0.5  ⍝sqrt

⎕ {quad} ⍞ {quotequad}

    NB. In the J session...
] a=: expr  NB. ⎕←a←expr
[ a=: expr  NB. ...also works.
    NB. Inside an explicit definition...
smoutput expr
smoutput a=: expr

÷ {reciprocal}

% b         NB. (Reciprocal) ÷b

See also: {divide} {reciprocal}

⌽ {reverse}

|. a        NB. ⌽a

See also: {phi} {theta}

⍴ {rho}

$ a          NB. (Shape Of)  ⍴a
# a          NB. (Tally)    ↑⍴a
b $ a        NB. (Shape) APL: (Reshape) b⍴a
b # a        NB. (Copy)  b copies of elements of a
   5#'a'
aaaaa
   5$'a'
aaaaa
   5#'abc'
aaaaabbbbbccccc
   5$'abc'
abcab
,. i.n       NB. (RavelItems) (n 1)⍴⍳n
,: i.n       NB. (Itemize)    (1 n)⍴⍳n

See also: {slash} {slashbar}

? {roll} {deal}

 ? b         NB. (Roll) random number from: i.b
 ?. b        NB. ditto, fixed ⎕RL
 a ? b       NB. (Deal) (a) non-repeats dealt from: i.b

⌽ {rotate}

b |. a      NB. b⌽a

See also: {phi} {theta}

× {signum}

* a          NB. ×a

See also: {times}

/ {slash} ⌿ {slashbar}

+/ a         NB. +⌿a ⍝ 2D
+/"1 a       NB. +/a
1 0 1 #'abc' NB. 1 0 1/'abc'

⌷ {squad}

i { a        NB. i⌷a

* {star}

See also: {power}

↑ {take} ↓ {drop}

{. a         NB. ↑a
{: a         NB. ↑¯1↑a
n {. a       NB. n↑a
}. a         NB. 1↓a
}: a         NB. ¯1↓a
n }. a       NB. n↓a

~ {tilde}

-. b         NB. (Not) ~b
b -. a       NB. (Less) b~a

× {times} {signum}

* a          NB. (signum) ×a
b * a        NB. b×a
*: a         NB. a×a or a*2

⍉ {transpose}

|: a         NB. ⍉a
n |: a       NB. n⍉a

{union} {intersect}

Undefined in the leading APLs.

⍋ {upgrade} ⍒ {downgrade}

/: a         NB. ⍋a
\: a         NB. ⍒a

⍬ {zilde}

0$0          NB. ⍬
i.0          NB. ditto
a:           NB. (Ace) ⊂⍬

⎕AV

a.

⎕CR

5!:5 <'nub'  NB. ⎕CR 'nub'

⎕CT

a =!.t b     NB. ⎕CT t ⋄ a = b

⎕DL

6!:3 (2.5)   NB. ⎕DL 2.5

⎕EX

4!:55 <'foo'  NB. ⎕EX 'a'
erase 'foo'
erase 'foo baa'

⎕FMT

 n". numstring    NB. interprets numstring as conventional (as well as J) numerals
                  NB. replacing bad numerals with number: n
 0". numstring    NB. like APL+, replaces with 0
_.". numstring    NB. replaces with _.

⎕FX

3 : defstring
13 : defstring    NB. attempts tacit defn from explicit defstring
4 : defstring
see also: {del}

⎕LIB

1!:0 <path   NB. ⎕LIB path

⎕LOAD

load 'myscript.ijs'
require 'myscript.ijs'  NB. loads script if not already loaded
load 'files'            NB. same as: load '~system/main/files.ijs'

⎕LX

Simply insert a statement to run the app at the bottom of the script

⎕NC

4!:0 <'foo'
nameclass <'foo'

⎕NL

nl 0       NB. ⎕NL 2
nl 3       NB. ⎕NL 3

⎕RL

9!:0 ''    NB. query random seed
9!:1 ]int  NB. set random seed
see: {roll} {deal}

⎕SAVE

There is no "workspace" in J. Apps are kept in one or more J scripts (*.ijs)

If you really need to dump the contents of a locale to a J script, see: LOBROW.

⎕TC

CR,LF,CRLF

⎕TS

6!:0 ''         NB. 6 entries, no millisecs
6!:0 'hh:mm:ss'

⎕WA ⎕WSSIZE

7!:0 '' 	NB. Current. Space currently in use.
7!:3 '' 	NB. table of blocks available

⎕WSID

See note at ⎕SAVE

:End :EndIf :EndSelect ...

end.            NB. never varies

:For

for. i.n do. EXPR end.  NB. :For x :In ⍳n ⋄ EXPR ⋄ :End  ⍝x unused
for_i. T do. EXPi end.  NB. :For i :In T ⋄ EXPi ⋄ :End
NB. loop may contain:
  break.          NB. terminates loop
  continue.       NB. terminates current pass

:If :ElseIf :Else

if. T do. EXPR else. EEXPR end.

if. T do. EXPR
elseif. T2 do. EXPR2
elseif. do. EXPR3   NB. cannot use: else. after elseif.
end.

See Control Structures

:Repeat

(+: ^: 3 ) n    NB. applies: +: (=Double) 3 times to n
                NB. another way: use Gerunds
^:_             NB. repeats until nothing (effectively) changes

:Select :Case :CaseList :Else

select. a
fcase. a1 do. EXPR1    NB. fcase. falls thru (use for :CaseList)
 case. a2 do. EXPR2
 case. do. EXPR        NB. catches all other cases (use for :Else)
end.

:Try :Catch

try.
  NB. code here
catch.
  NB. code to execute on J error
end.

f :: 0: y     NB. if f crashes, carry on regardless
z=: x f::g y  NB. if f crashes, do g instead

:While

while. B do. EXPR end.   NB. EXPR not executed if test: B fails
whilst. B do. EXPR end.  NB. EXPR executed at least once, first time without testing B (avoids need to initialise)

Displaying APL symbols correctly

Chars specific to APL for reference use:

←↑→↓∆∇∊∘∧∨∩∪∼≡≢≤≥⊂⊃⊖⊢⊣⊤⊥⋄⌈⌊⌶⌷⌹⌽⌿⍀⍉⍋⍎⍒⍕⍝⍞⍪⍳⍴⍵⍷⍺⎕○×÷

Copy/paste individual chars to a J window, if needed in a comment.

The above code-section should look like this (snapshotted on a Macintosh):

Aplsnapshot.jpg

If that's not what you see, then take a look at Typesetting/APL Fonts for advice on how to proceed. APL C.6.1NuVoc R.1