ρ(V)=[size(V)...]
ι(n)=[1:n[1]]
ι(V,W)=[v∈W ? minimum(findin(W,v)) : 1+length(W) for v in V]
∘(*::Function,V,W)=[v*w for v in V, w in W]
⍋(V)=sortperm(V)
↓(n,V)=V[n+1:end]
⍉(V)=V.'
⍉(V,P)=permutedims(V,P) #APL allowed repeated elements in P
f=(V,n)->(mod(n,V),(n-mod(n,V))/V)
# APL encode function generalizes julia's digits
function ⊤(V,state)
z=copy(V)
for i=length(V):-1:1
z[i], state = f(z[i], state)
end
z
end
⊤(V,n::Array)=[[⊤(V,nn)' for nn in n]...]'
Problems taken directly from https://cs.uwaterloo.ca/~shallit/Talks/APL50-talk2.pdf
# Distinct elements from a list
V = [3, 2, 1, 3, 4, 2, 1, 7, 4, 2, 2, 3]
X = V[ ( ι(ρ(V)).== ι(V,V) ) ]
5-element Array{Int64,1}: 3 2 1 4 7
# Julia
unique(V)
5-element Array{Int64,1}: 3 2 1 4 7
# Frequency Distribution
[X sum(∘(==,X,V),2)]
5x2 Array{Int64,2}: 3 3 2 4 1 2 4 2 7 1
⊤([10,10,10],[877,123,43])
3x3 Array{Int64,2}: 8 1 0 7 2 4 7 3 3
⊤([1760,3,12],95) # 95 inches = 2 yards, 1 foot, 11 inches
3-element Array{Int64,1}: 2 1 11
P=[2, 3, 7] # List of primes
E=[2, 1, 2] # List of exponents
prod(P.^E)
588
prod(E+1) # Number of divisors
18
# List of divisors
prod(P.^⊤(E+1 ,[-1+ι(prod(E+1))] ),1)
1x18 Array{Int64,2}: 1 7 49 3 21 147 2 14 98 6 42 294 4 28 196 12 84 588
# Julia: list of divisors
d=Int64[]
for i1 in 0:E[1], i2 in 0:E[2], i3 in 0:E[3]
push!(d,prod(P.^[i1, i2, i3]))
end
d'
1x18 Array{Int64,2}: 1 7 49 3 21 147 2 14 98 6 42 294 4 28 196 12 84 588
⍋([13, 8, 122, 4 ])
4-element Array{Int64,1}: 4 2 1 3
# Distributing elements along diagonals
reshape(⍋(⍋(∘(+,ι(6),ι(5))[:])),6,5)'
5x6 Array{Int64,2}: 1 2 4 7 11 16 3 5 8 12 17 21 6 9 13 18 22 25 10 14 19 23 26 28 15 20 24 27 29 30
# Julia (nothing fancy)
z=zeros(Int64,5,6)
c=0
for d=1:5, i=1:d z[i,d+1-i]=c+=1 end
c=31
for d=1:5, i=1:d z[6-i,6-d+i]=c-=1 end
z
5x6 Array{Int64,2}: 1 2 4 7 11 16 3 5 8 12 17 21 6 9 13 18 22 25 10 14 19 23 26 28 15 20 24 27 29 30
# Guess Linear Recurrence
V=[1,2,3,5,8,13,21,34]
V[∘(+ ,ι(ρ(V)-2), ι(2)-1)] \ ↓(2,V)
2-element Array{Float64,1}: 1.0 1.0
# Julia
[V[1:end-2] V[2:end-1]]\V[3:end]
2-element Array{Float64,1}: 1.0 1.0
W =[3,7,20,79,310,1201]
W[∘(+ ,ι(ρ(W)-3), ι(3)-1)] \ ↓(3,W)
3-element Array{Float64,1}: 2.0 -1.0 4.0
# Julia
[W[1:end-3] W[2:end-2] W[3:end-1]]\W[4:end]
3-element Array{Float64,1}: 2.0 -1.0 4.0
Y=[1900,1962,2000,2012]
# Determining Leap Years
[reduce(!=,(0.==∘(mod,Y,[4,100,400]))[i,:]) for i=1:4 ]
4-element Array{Any,1}: false false true true
Y=[1900,1962,2000,2012]
LY(x)= (mod(x,4)==0 && mod(x,100)!=0) || mod(x,400)==0
map(LY,Y)
4-element Array{Bool,1}: false false true true
# Dijkstra's Question
M=[3 16 10 11;5 1 14 14;19 8 6 17;1 2 11 14; 1 8 2 9; 14 12 19 17]
6x4 Array{Int64,2}: 3 16 10 11 5 1 14 14 19 8 6 17 1 2 11 14 1 8 2 9 14 12 19 17
M[M.==∘(+,map(ι,ρ(M))...)]
2-element Array{Int64,1}: 6 9
# Julia
for i=1:6, j=1:4
if M[i,j]==i+j println(M[i,j]) end
end
6 9
# Getting Definitions Correct
@show 0^0
@show -8^(1/3)
@show sum(ι(0))
@show prod(ι(0));
0^0 => 1 -(8^(1 / 3)) => -2.0 sum(ι(0)) => 0 prod(ι(0)) => 1
⍉(M)
4x6 Array{Int64,2}: 3 5 19 1 1 14 16 1 8 2 8 12 10 14 6 11 2 19 11 14 17 14 9 17
⍉(M,[1,2])
6x4 Array{Int64,2}: 3 16 10 11 5 1 14 14 19 8 6 17 1 2 11 14 1 8 2 9 14 12 19 17
⍉(M,[2,1])
4x6 Array{Int64,2}: 3 5 19 1 1 14 16 1 8 2 8 12 10 14 6 11 2 19 11 14 17 14 9 17