{"id":410,"date":"2004-09-17T23:03:00","date_gmt":"2004-09-17T23:03:00","guid":{"rendered":"http:\/\/www.dresan.com\/blog\/?p=410"},"modified":"2004-09-17T23:03:00","modified_gmt":"2004-09-17T23:03:00","slug":"my-first-apl-program","status":"publish","type":"post","link":"https:\/\/dresan.com\/blog\/2004\/09\/17\/my-first-apl-program\/","title":{"rendered":"My First APL Program"},"content":{"rendered":"<p>As a side project right now I&#8217;m investigating computer languages &#8212; not just experimenting with Perl vs. Python but trying to expose myself to different <i>families<\/i> of languages, such as functional, logic, imperative, object oriented, and obfuscated.<\/p>\n<p>Most recently, someone mentioned that <i>array<\/i> languages are the least<br \/>\n<br \/>widely known family &#8212; perhaps because the founding language in the group,<br \/>\n<br \/>APL, was written with a nonstandard character set.  Perhaps the most terse<br \/>\n<br \/>of all programming languages, APL spawned a series of children, like J and K,<br \/>\n<br \/>which, even though they can be written in a normal alphabet, retain APL&#8217;s<br \/>\n<br \/>essential terseness &#8212; or, dare we say, obscurity?<\/p>\n<p>I plan to learn J.  However, before I did so, I committed myself to learning<br \/>\n<br \/>at least a smidgen of the original APL, so I could see how the language was<br \/>\n<br \/>originally intended to work and look.<\/p>\n<p>Here&#8217;s my first APL program, designed to produce a Vigenere tableau:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.dresan.com\/images\/MyFirstAplProgram.jpg\" \/><\/p>\n<p>Note the code isn&#8217;t in the standard ASCII character set, so I had<br \/>\n<br \/>to represent it as an image file.<\/p>\n<p>The heart of the code is the central box of the next image. This should read as:<\/p>\n<blockquote><p>\n<\/p>\n<pre>\n<br \/>disclose (( -1 rotate (indexList (shapeOf Y)))\n<br \/>   (outerProduct rotate) (enclose Y))\n<br \/><\/pre>\n<p><\/p><\/blockquote>\n<p>and executes right to left:<\/p>\n<div align=\"center\">\n<br \/><a href=\"http:\/\/www.dresan.com\/images\/MyFirstAplProgramExploded.pdf\"><br \/>\n<br \/><img decoding=\"async\" src=\"http:\/\/www.dresan.com\/images\/MyFirstAplProgramExploded.jpg\" \/><br \/>\n<br \/><font size=\"-2\">Click for PDF<\/font><br \/>\n<br \/><\/a><br \/>\n<\/div>\n<p>To translate into pseudocode:<\/p>\n<pre>\n<br \/>\/\/ Assign the alphabet to the variable Y\n<br \/>Y = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n<br \/>&nbsp;\n<br \/>\/\/ Now perform the following:\n<br \/>(unbox\n<br \/> ((outerproduct rotate)\n<br \/>  (rotate -1 (enumerate (length y)))\n<br \/>  (box y)\n<br \/>  )\n<br \/> )\n<br \/>&nbsp;\n<br \/>\/\/ This is the moral equivalent of:\n<br \/>\/\/ (1). Treat array Y like a \"scalar\" variable\n<br \/>boxedY = box(y)\n<br \/>&nbsp;\n<br \/>\/\/ (2) Find the length of Y - in this case,26\n<br \/>lengthOfY = length(y)\n<br \/>&nbsp;\n<br \/>\/\/ (3) Get an array from 1 to the length of Y\n<br \/>\/\/     We will use this (1 2 ... 26) later to\n<br \/>\/\/     rotate the alphabet to the side\n<br \/>list1toY = enumerate(lengthOfY)\n<br \/>&nbsp;\n<br \/>\/\/ (4) Rotate list by one so that the first\n<br \/>\/\/     row is a \"no-op\" rotation: (26 1 2 ... 25)\n<br \/>\/\/     (I should have just have subtracted one\n<br \/>\/\/     from each list to get (0 1 .. 25), but\n<br \/>\/\/     it's just my first APL program!)\n<br \/>rotateAmounts = rotate(-1, list1toY)\n<br \/>&nbsp;\n<br \/>\/\/ (5) Create a \"mapping rotate\" function that takes\n<br \/>\/\/     a list of integers as its first argument and\n<br \/>\/\/     rotates each element of its second argument\n<br \/>\/\/     by the supplied integer.\n<br \/>\/\/     We had to \"box\" Y earlier because APL by\n<br \/>\/\/     default treat each of its elements as a one-element\n<br \/>\/\/     array.\n<br \/>mapRotate = outerproduct(rotate)\n<br \/>&nbsp;\n<br \/>\/\/ Now create 26 copies of Y, each rotated by the amount\n<br \/>\/\/ specified in the rotateAmounts variable, and collect\n<br \/>\/\/ them all into a list.  This is morally equivalent to:\n<br \/>\/\/ list(\n<br \/>\/\/      rotate(26, boxedY),\n<br \/>\/\/      rotate( 1, boxedY),\n<br \/>\/\/      ...\n<br \/>\/\/      rotate(25, boxedY)\n<br \/>\/\/      )\n<br \/>rotatedY =  mapRotate(rotateAmounts, boxedY)\n<br \/>&nbsp;\n<br \/>\/\/ Now \"unbox\" the rotated list, which takes the\n<br \/>\/\/ list of lists of rotated Ys and turns them into\n<br \/>\/\/ a matrix or grid rotation\n<br \/>return unbox(rotatedY)\n<br \/><\/pre>\n<p><\/p>\n<p>\n<br \/>The idea in my mind was to take an input array and print<br \/>\n<br \/>a diagonalized rectangle with it.  With the alphabet, this<br \/>\n<br \/>becomes a Vigenere table &#8212; once the &#8220;indecipherable cipher&#8221;,<br \/>\n<br \/>now a trivial matter for any modern computer.<\/p>\n<p>With a different string, like &#8220;01&#8221; or &#8220;_[]&#8221; (reading the two<br \/>\n<br \/>brackets as the APL Quad character), and a suitable change<br \/>\n<br \/>in length of the output, it becomes instead a checkerboard:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.dresan.com\/images\/AplCheckerboard.jpg\" \/><\/p>\n<p>Deciphering that is left as an exercise to you, dear fanu.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a side project right now I&#8217;m investigating computer languages &#8212; not just experimenting with Perl vs. Python but trying to expose myself to different families of languages, such as&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11],"class_list":["post-410","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-development","ratio-2-1","entry"],"_links":{"self":[{"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":0,"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"wp:attachment":[{"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dresan.com\/blog\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}