Fear and loathing with APL (oredev)

850 views

Published on

APL is an interesting beast, with an immense level of expressiveness locked away in a syntax that instantly scares away all but the initiated.

In this talk, we'll take a gentle introduction to APL and demonstrate its power to implement complex algorithms in very little code. We'll compare examples with equivalents in C# to illustrate why APL is much better suited as a "thinking tool" for programmers.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
850
On SlideShare
0
From Embeds
0
Number of Embeds
604
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Fear and loathing with APL (oredev)

  1. 1. Fear and Loathing with APL
  2. 2. Hi, my name is Yan Cui aka @theburningmonk
  3. 3. What is APL? created at IBM in 1957, by Ken Iverson designed to simplify math notations “A Programming Language”
  4. 4. What is APL? famed for use of special symbols array programming influenced Mathematica, Matlab, etc.
  5. 5. hey, I’m learning APL
  6. 6. what the hell is an APL?
  7. 7. have you lost your mind?
  8. 8. Conway’s Game of Life https://www.youtube.com/watch?v=a9xAKttWgP4
  9. 9. “Beauty is in the Eye of the Beholder” beauty doesn't exist on its own but is created by observers
  10. 10. “…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
  11. 11. “…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
  12. 12. http://bit.ly/2eM3Qvr
  13. 13. “A language that doesn't affect the way you think about programming, is not worth knowing.” - Alan Perlis
  14. 14. “in an era of mainstream programming languages, APL is a rare opportunity to truly challenge the way you think about programming” - me
  15. 15. Let’s learn some APL http://tryapl.org/
  16. 16. 2 + 2 4 4 - 1 3 + and - works the way you expect
  17. 17. 2 * 3 8 4 * 4 256 but * means power
  18. 18. 2 / 3 3 3 and / means replicate it means replicate the scalar value 3, 2 times and returns an array of 2 elements
  19. 19. 2 × 2 4 4 ÷ 2 2 multiplication (×) and division (÷) just like in Maths!
  20. 20. x ← 42 y ← 1 2 3 4 assignment (←) variable names are case sensitive
  21. 21. variables are mutable x ← 42 x ← 1 2 3 4 x 1 2 3 4
  22. 22. assign multiple values at once (kinda like pattern matching) x y z ← 1 2 3 x 1 y 2
  23. 23. but the patterns must match x y ← 1 2 3 LENGTH ERROR x y ← 1 2 3
  24. 24. 1 2 3 - 3 2 1 ¯2 0 2 1 2 3 × 3 2 1 3 4 3 arithmetic operations work on arrays too
  25. 25. 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
  26. 26. 4 ⌊ 2 2 4 ⌈ 2 4 min (⌊) and max (⌈)
  27. 27. 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
  28. 28. dyadic 2 ⌈ 4 1 2 ⌈ 3 1 monadic ⌈ 2 ⌈ 1 3
  29. 29. ⌊ 3 4.1 4.5 5.6 3 4 4 5 monadically, ⌊ means floor
  30. 30. ⌈ 3 4.1 4.5 5.6 3 5 5 6 and ⌈ means ceiling
  31. 31. 3 2 ⍴ 1 2 3 4 5 6 1 2 3 4 5 6 dyadically, ⍴ reshapes right hand side rows columns
  32. 32. ⍴ 1 2 3 3 ⍴ (3 2 ⍴ 1 2 3 4 5 6) 3 2 monadically, ⍴ returns shape of argument
  33. 33. 3 2 ⍴ 1 2 3 1 2 3 1 2 3 ⍴ recycles elements to fill space
  34. 34. 2 2 ⍴ 1 2 3 4 5 6 1 2 3 4 and ignores additional elements
  35. 35. 3 3 ⍴ 1 0 0 0 1 0 0 0 1 0 0 0 1 here’s a neat trick to get diagonals :-P
  36. 36. ⍴ 2 2 ⍴ 0 1 0 2 2 ⍴ (2 2 ⍴ 0 1 0) 2 2 oh, and APL is read from RIGHT-TO-LEFT
  37. 37. ⍴ 2 2 ⍴ 0 1 0 2 2 ⍴ (2 2 ⍴ 0 1 0) 2 2 oh, and APL is read from RIGHT-TO-LEFT
  38. 38. +/ 1 2 3 6 btw, / also means reduce ie, reduce over the array [1, 2, 3] with the plus function
  39. 39. ×/ 1 2 3 4 5 120 btw, / also means reduce and here’s factorials!
  40. 40. Defined Functions
  41. 41. Avg ← { (+/ ⍵) ÷ ⍴⍵ } anatomy of a defined function function name assignment function body
  42. 42. Avg ← { (+/ ⍵) ÷ ⍴⍵ } Avg 1 2 3 anatomy of a defined function omega ⍵ represents the right argument
  43. 43. Diff ← { (+/ ⍺) - (+/ ⍵) } 3 4 5 Diff 1 2 3 anatomy of a defined function alpha ⍺ represents the left argument
  44. 44. Plus ← { ⍺ + ⍵ } Plus/ 1 2 3 6 defined functions can be used with / too
  45. 45. Indexing an Array
  46. 46. Xs ← 1 2 3 Xs[2] 2 APL is one-indexed!
  47. 47. Xs[1 2 1] 1 2 1 Xs[2 2 ⍴ 1 2] 1 2 1 2 indices can be array too
  48. 48. Xs[3 2] ← 6 4 Xs 1 4 6 updating an array
  49. 49. ⍳9 1 2 3 4 5 6 7 8 9 index generator
  50. 50. Booleans
  51. 51. 1 < 2 1 1 2 3 < 3 2 1 1 0 0 1 is true, 0 is false yup, works with arrays too ;-)
  52. 52. ~1 0 ~1 0 1 0 1 0 monadically, ~ means negate
  53. 53. (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 ∨
  54. 54. (⍳10) > 7 0 0 0 0 0 0 0 1 1 1 +/ (⍳10) > 7 3 count no. of positives
  55. 55. (⍳10) > 7 0 0 0 0 0 0 0 1 1 1 ⍳10 > 7 ? are these two expression equivalent?
  56. 56. (⍳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!
  57. 57. (⍳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?
  58. 58. Compression (aka filtering)
  59. 59. 1 0 1 / 3 2 1 3 1 1 0 1 / 'iou' i u booleans can be used to as a mask
  60. 60. 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
  61. 61. 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
  62. 62. 1 2 3 ~ 3 4 3 5 1 2 ‘ab’ ‘cd’ ‘ef’ ~ ‘cd’ ‘ef’ ab dyadically, ~ means without
  63. 63. 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
  64. 64. Phrase ← ‘hello world’ (Phrase ∊ 'aeiou') / Phrase eoo which vowels appeared in a phrase?
  65. 65. 2 2 ⍴ 1 2 1 2 1 2 ∊ 2 2 ⍴ 1 2 1 2 1 2 monadically, ∊ flattens a matrix
  66. 66. Take & Drop
  67. 67. 2 ↑ 1 2 3 4 1 2 2 2 ↑ (3 3 ⍴ ⍳9) 1 2 4 5 ↑ to take items from an array !?
  68. 68. 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9
  69. 69. 2 2 ↑ 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9 take a 2 (rows) by 2 (cols) matrix
  70. 70. 2 ↓ 1 2 3 4 3 4 2 2 ↓ (3 3 ⍴ ⍳9) 9 ↓ to drop items from an array !?
  71. 71. 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9
  72. 72. 2 2 ↓ 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9 drop first 2 rows and 2 cols
  73. 73. ¯2 ↑ 1 2 3 4 3 4 ¯2 ↓ 1 2 3 4 1 2 use negative count to take/drop from end of array
  74. 74. ¯2 2 ↑ (3 3 ⍴ ⍳9) ? ¯2 2 ↓ (3 3 ⍴ ⍳9) ? what about these? http://tryapl.org/ try it out yourself
  75. 75. Mirrors & Transportations
  76. 76. 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9 given a matrix
  77. 77. 3 3 ⍴ ⍳9 3 2 1 6 5 4 9 8 7 mirrors the matrix horizontally
  78. 78. 3 3 ⍴ ⍳9 7 8 9 4 5 6 1 2 3 mirrors the matrix vertically
  79. 79. ⍉ 3 3 ⍴ ⍳9 1 4 7 2 5 8 3 6 9 transposes the matrix⍉
  80. 80. Outer Products
  81. 81. cartesian product
  82. 82. 1 2 3 .+ 4 5 6 5 6 7 6 7 8 7 8 9 . creates a cartesian product
  83. 83. 1 2 3 .× 4 5 6 4 5 6 8 10 12 12 15 18 . creates a cartesian product
  84. 84. (⍳3) .= (⍳3) 1 0 0 0 1 0 0 0 1 . creates a cartesian product
  85. 85. (⍳3) .< (⍳3) 0 1 1 0 0 1 0 0 0 . creates a cartesian product
  86. 86. (⍳3) .⌈ (⍳3) 1 2 3 2 2 3 3 3 3 . creates a cartesian product
  87. 87. (⍳3) .+ (⍳3) 2 3 4 3 4 5 4 5 6 . creates a cartesian product
  88. 88. what about this? http://tryapl.org/ try it out yourself ∊ (⍳3) .× (⍳5) ?
  89. 89. Demo Time…
  90. 90. Conclusions
  91. 91. should I learn APL? absolutely! it’s the shock to the system your brain needs
  92. 92. is it gonna help my career? does it matter?
  93. 93. @theburningmonk theburningmonk.com github.com/theburningmonk

×