fear and loathing with apl (oredev)
TRANSCRIPT
Fear and Loathing with APL
Hi, my name is Yan Cuiaka @theburningmonk
What is APL?
created at IBM in 1957, by Ken Iverson
designed to simplify math notations
“A Programming Language”
What is APL?
famed for use of special symbols
array programming
influenced Mathematica, Matlab, etc.
hey, I’m learning
APL
what the hell is an
APL?
have you lost your
mind?
Conway’s Game of Life
https://www.youtube.com/watch?v=a9xAKttWgP4
“Beauty is in the Eye of the Beholder”
beauty doesn't exist on its own but is created by observers
“…We followed up with two studies on the accuracy rates of novices using a total of six programming languages: Ruby, Java, Perl, Python, Randomo, and Quorum. Randomo was designed by randomly choosing some keywords from the ASCII table (a metaphorical placebo). To our surprise, we found that languages using a more traditional C-style syntax (both Perl and Java) did not afford accuracy rates significantly higher than a language with randomly generated keywords, …”
“An Empirical Investigation into Programming Language Syntax”- Andreas Stefik and Susanna Siebert
“…We followed up with two studies on the accuracy rates of novices using a total of six programming languages: Ruby, Java, Perl, Python, Randomo, and Quorum. Randomo was designed by randomly choosing some keywords from the ASCII table (a metaphorical placebo). To our surprise, we found that languages using a more traditional C-style syntax (both Perl and Java) did not afford accuracy rates significantly higher than a language with randomly generated keywords, …”
“An Empirical Investigation into Programming Language Syntax”- Andreas Stefik and Susanna Siebert
http://bit.ly/2eM3Qvr
“A language that doesn't affect the way you think
about programming, is not worth knowing.”
- Alan Perlis
“in an era of mainstream programming languages, APL is a rare opportunity to truly challenge the way you think about
programming”
- me
2 + 2 4
4 - 1 3
+ and - works the way you expect
2 * 3 8
4 * 4 256
but * means power
2 / 3 3 3
and / means replicate
it means replicate the scalar value 3, 2 timesand returns an array of 2 elements
2 × 2 4 4 ÷ 2 2
multiplication (×) and division (÷)
just like in Maths!
x ← 42 y ← 1 2 3 4
assignment (←)
variable names are case sensitive
variables are mutable
x ← 42 x ← 1 2 3 4 x
1 2 3 4
assign multiple values at once(kinda like pattern matching)
x y z ← 1 2 3 x
1 y
2
but the patterns must match
x y ← 1 2 3 LENGTH ERROR x y ← 1 2 3
1 2 3 - 3 2 1 ¯2 0 2
1 2 3 × 3 2 1 3 4 3
arithmetic operations work on arrays too
1 2 3 + 1 2 3 4
2 × 1 2 3 2 4 6
if one side is scalar then the other side can be any shape
4 ⌊ 2 2 4 ⌈ 2 4
min (⌊) and max (⌈)
1 2 3 4 5 ⌈ 4 4 4 4 4 5 1 2 3 ⌈ 3 2 1 3 2 3
min (⌊) and max (⌈)
same rules apply for arrays
dyadic
2 ⌈ 4 1 2 ⌈ 3 1
monadic
⌈ 2 ⌈ 1 3
⌊ 3 4.1 4.5 5.6 3 4 4 5
monadically, ⌊ means floor
⌈ 3 4.1 4.5 5.6 3 5 5 6
and ⌈ means ceiling
3 2 ⍴ 1 2 3 4 5 6 1 2 3 4 5 6
dyadically, ⍴ reshapes right hand side
rows
columns
⍴ 1 2 3 3 ⍴ (3 2 ⍴ 1 2 3 4 5 6)
3 2
monadically, ⍴ returns shape of argument
3 2 ⍴ 1 2 3 1 2 3 1 2 3
⍴ recycles elements to fill space
2 2 ⍴ 1 2 3 4 5 6 1 2 3 4
and ignores additional elements
3 3 ⍴ 1 0 0 0 1 0 0 0 1 0 0 0 1
here’s a neat trick to get diagonals :-P
⍴ 2 2 ⍴ 0 1 0 2 2 ⍴ (2 2 ⍴ 0 1 0)
2 2
oh, and APL is read from RIGHT-TO-LEFT
⍴ 2 2 ⍴ 0 1 0 2 2 ⍴ (2 2 ⍴ 0 1 0)
2 2
oh, and APL is read from RIGHT-TO-LEFT
+/ 1 2 3 6
btw, / also means reduce
ie, reduce over the array [1, 2, 3] with the plus function
×/ 1 2 3 4 5 120
btw, / also means reduce
and here’s factorials!
Defined Functions
Avg ← { (+/ ⍵) ÷ ⍴⍵ }
anatomy of a defined functionfunction name
assignment function body
Avg ← { (+/ ⍵) ÷ ⍴⍵ }
Avg 1 2 3
anatomy of a defined function
omega ⍵ represents the right argument
Diff ← { (+/ ⍺) - (+/ ⍵) }
3 4 5 Diff 1 2 3
anatomy of a defined function
alpha ⍺ represents the left argument
Plus ← { ⍺ + ⍵ } Plus/ 1 2 3
6
defined functions can be used with / too
Indexing an Array
Xs ← 1 2 3 Xs[2]
2
APL is one-indexed!
Xs[1 2 1] 1 2 1
Xs[2 2 ⍴ 1 2] 1 2 1 2
indices can be array too
Xs[3 2] ← 6 4 Xs
1 4 6
updating an array
⍳9 1 2 3 4 5 6 7 8 9
index generator
Booleans
1 < 2 1
1 2 3 < 3 2 1 1 0 0
1 is true, 0 is false
yup, works with arrays too ;-)
~1 0
~1 0 1 0 1 0
monadically, ~ means negate
(1 2 3 < 3 2 1) ∧ (1 1 1) 1 0 0
(1 2 3 < 3 2 1) ∨ (1 1 1) 1 1 1
logical AND ∧ and logical OR ∨
(⍳10) > 7 0 0 0 0 0 0 0 1 1 1
+/ (⍳10) > 7 3
count no. of positives
(⍳10) > 7 0 0 0 0 0 0 0 1 1 1 ⍳10 > 7
?
are these two expression equivalent?
(⍳10) > 7 0 0 0 0 0 0 0 1 1 1 ⍳10 > 7
1
are these two expression equivalent?
nope! remember, APL reads from RIGHT-TO-LEFT!
(⍳10) > 7 0 0 0 0 0 0 0 1 1 1
7 < ⍳10 0 0 0 0 0 0 0 1 1 1
are these two expression equivalent?
Compression (aka filtering)
1 0 1 / 3 2 1 3 1
1 0 1 / 'iou' i u
booleans can be used to as a mask
Values ← 1 3 5 7 9 Values > 4
0 0 1 1 1 (Values > 4) / Values
5 7 9
a LINQ-style Where can be written as
Values > 4 0 0 1 1 1 ⍳⍴Values
1 2 3 4 5 (Values > 4) / (⍳⍴Values)
3 4 5
and to get the indices instead
1 2 3 ~ 3 4 3 5 1 2
‘ab’ ‘cd’ ‘ef’ ~ ‘cd’ ‘ef’ ab
dyadically, ~ means without
1 1 3 5 3 7 ∊ 1 2 3 1 1 1 0 1 0
'hello world' ∊ 'aeiou' 0 1 0 0 1 0 0 1 0 0 0
dyadically, ∊ checks for membership
Phrase ← ‘hello world’ (Phrase ∊ 'aeiou') / Phrase
eoo
which vowels appeared in a phrase?
2 2 ⍴ 1 2 1 2 1 2
∊ 2 2 ⍴ 1 2 1 2 1 2
monadically, ∊ flattens a matrix
Take & Drop
2 ↑ 1 2 3 4 1 2
2 2 ↑ (3 3 ⍴ ⍳9) 1 2 4 5
↑ to take items from an array
!?
3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9
2 2 ↑ 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9
take a 2 (rows) by 2 (cols) matrix
2 ↓ 1 2 3 4 3 4
2 2 ↓ (3 3 ⍴ ⍳9) 9
↓ to drop items from an array
!?
3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9
2 2 ↓ 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9
drop first 2 rows and 2 cols
¯2 ↑ 1 2 3 4 3 4
¯2 ↓ 1 2 3 4 1 2
use negative count to take/drop from end of array
¯2 2 ↑ (3 3 ⍴ ⍳9) ?
¯2 2 ↓ (3 3 ⍴ ⍳9) ?
what about these?
http://tryapl.org/ try it out yourself
Mirrors & Transportations
3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9
given a matrix
3 3 ⍴ ⍳9 3 2 1 6 5 4 9 8 7
mirrors the matrix horizontally
3 3 ⍴ ⍳9 7 8 9 4 5 6 1 2 3
mirrors the matrix vertically
⍉ 3 3 ⍴ ⍳9 1 4 7 2 5 8 3 6 9
transposes the matrix⍉
Outer Products
cartesian product
1 2 3 .+ 4 5 6 5 6 7 6 7 8 7 8 9
. creates a cartesian product
1 2 3 .× 4 5 6 4 5 6 8 10 12 12 15 18
. creates a cartesian product
(⍳3) .= (⍳3) 1 0 0 0 1 0 0 0 1
. creates a cartesian product
(⍳3) .< (⍳3) 0 1 1 0 0 1 0 0 0
. creates a cartesian product
(⍳3) .⌈ (⍳3) 1 2 3 2 2 3 3 3 3
. creates a cartesian product
(⍳3) .+ (⍳3) 2 3 4 3 4 5 4 5 6
. creates a cartesian product
Demo Time…
Conclusions
should I learn APL?
absolutely! it’s the shock to the system your brain needs
is it gonna help my career?
does it matter?
@theburningmonktheburningmonk.comgithub.com/theburningmonk