In this series of articles, I will be documenting my process in implementing an APL in Haskell. I've been learning APL for the past year and since the first day I've been wondering how difficult it might be to write an interpreter for a simplified dialect, named TinyAPL. In a somewhat bold choice, I decided to delay parsing until a few articles and weeks into the future. I want to start by implementing the part I'm most interested in, that is, a representation of APL arrays, functions and operators in the Haskell typesystem. module TinyAPL where Prerequisites Sadly, this series can't be an introduction to Haskell, or it would become way too long. I therefore assume some level of familiarity with the language. If you need help understanding a particular topic, or you think it might be clearer, let me know! (More information at the end of the article.) Following along All the code in this series is written for GHC 9.6.2. ghcup should help you set that up, although I'm sure not much modification will be needed to make my code compile on later versions. A GitHub repository is also available with all the code from this article. Representing Arrays TinyAPL only supports characters and (complex) numbers as scalar types, plus of course boxes. import Data.Complex data ScalarValue = Number (Complex Double) | Character Char | Box Array I'll store arrays as a pair of shape and elements, in ravel order. import Numeric.Natural data Array = Array { arrayShape :: [Natural], arrayContents :: [ScalarValue] } Because we cannot easily require that the invariant holds, I'll implement some helper functions to construct arrays. All functions assume that the invariant holds, and won't check. import Data.List scalar :: ScalarValue -> Array scalar x = Array [] [x] vector :: [ScalarValue] -> Array vector xs = Array [genericLength xs] xs arrayOf :: [Natural] -> [ScalarValue] -> Maybe Array arrayOf sh cs | product sh == genericLength cs = Just $ Array sh cs | otherwise = Nothing arrayReshaped :...
First seen: 2025-05-30 22:25
Last seen: 2025-05-30 22:25