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 J Reference Card 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

!

?

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

{alpha} {omega}

x ; y                 NB. (⍺ ⍵)

{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. ceiling ⌈
<.           NB. 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,b       NB. (Ravel)      APL: a⍪b
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

{commute}

a -~ b       NB. a -⍨ b

{deal}

{decode} {encode}

   b #. a       NB. (Base) b⊥a
   2 #. 1 0 1
5
   2 2 2 #. 1 0 1
5
   b #: a       NB. (Antibase) b⊤a
   #: 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
...

{diamond}

x=: 3 [ y=: 4

{disclose}

>a          NB. (Open) ⊃a

{divide} {reciprocal}

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

{domino}

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

{dot}

x +/ . * y  NB. Inner product

{each}

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

{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 localised

{hi-minus}

_1          NB. ¯1

{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

{jot}{dot}

a foo/ b    NB. a ∘.foo b

{lamp}

NB. ⍝ a comment

{left}, {right}

a [ b       NB. ⊣  lev/left
a ] b       NB. ⊢  dex/right

{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

{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 Rank carefully.

|."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}

] a=: expr  NB. ⎕←a←expr
[ a=: expr  NB. ...also works

{reciprocal}

% b         NB. (Reciprocal) ÷b

{reverse}

|. a        NB. ⌽a

{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

{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

{signum}

* a          NB. ×a

{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}

{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}

{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'

[]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

[]LIB

1!:0 <path   NB. ⎕LIB path

[]LOAD []SAVE

load 'myscript'
require

[]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

[]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

  NB. there is no workspace.

: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
elseif. T2 do. EXPR2
elseif. do. EXPR3   NB. cannot use: else. after elseif.
end.

: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.

: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)

APL chars

Chars specific to APL for reference use:

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

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.

APL2JPhraseBook (last edited 2012-02-16 16:37:15 by IanClark)