APL InterpreterJanuary 11, 2024Why APL?APL is an array programming language. Its only data type is the (multidimensional) array. While this might seem like a huge limitation, the generality it provides leads to a syntax that is incredibly compact and expressive, which forces the programmer to approach problems at a higher level.I was first drawn to APL by the nature of its syntax: with the exception of user-defined variables, all built-in functions and operators are single unicode symbols. As a result, code looks like this.⍝ My solution to day 7 of Advent of Code '23 c2n ← '23456789TJQKA'∘⍳ classify ← (5-≢⍤∪),(⌈/(+/∘.=⍨)),((5≡∪)×(⌈/c2n)) h2n ← 13⊥classify,(¯1∘+c2n) hands bids ← ↓⍉↑((' '∘≠)⊆⊢)¨ input bids ← ⍎¨ bids ⎕ ← +/ (⍳≢bids) × bids[⍋ h2n¨ hands] modes ← { ⍵≡⍬ : ⍬ ⋄ ∪⍵⌷⍨⊂(⍸⌈/=⊢)+/∘.=⍨⍵ } change_joker ← { (⊃ 'A' ,⍨ modes (⍵~'J'))@(⍸⍵='J') ⊢ ⍵ } c2n ← 'J23456789TQKA'∘⍳ h2n ← 13⊥(classify change_joker),(¯1∘+c2n) ⎕ ← +/ (⍳≢bids) × bids[⍋ h2n¨ hands]As you might expect, learning to write programs like this requires a totally different mind-set. Array programming is similar to functional programming – the primary way to control execution involves composition of functions – but APL tends to encourage the reliance on global properties and sweeping operations rather than low-level recursion.I’d like to think that learning to approach problems this way provides greater insights for programming in general.Why Haskell?I originally planned this project to be a deep-dive into APL, and learning Haskell was more of a side-quest. It ended up the other way around: the hardest aspect of this project, by far, was learning work with Haskell.In all honesty, Haskell probably isn’t an ideal tool for an array-language interpreter: it makes parsing and combination of functions much more elegant, but at the expense of ease of working with state, data structures, and performance.As a result, this project isn’t intended to be especially practically useful, nor to be a replacement for existi...
First seen: 2025-06-05 22:03
Last seen: 2025-06-06 16:07