TweetFollow Us on Twitter

Volume Number:1
Issue Number:12
Column Tag:APL Adventures

A Beginner's look at Mac APL

By Allyn Weaks, Seattle, WA.

I've wanted to learn APL ever since I heard that it could do integrals in one line and matrix inversions in a single symbol. Until PortaAPL for the Mac came along, I never had access to a computer with a full implementation. I'm hardly an expert at this language, having played with it for all of a month now, so please forgive any minor blunders. This month I'll start with a description of APL, enough of a tutorial to get you started defining simple functions (including an overview of the syntax, data types, and some of the simpler functions), and a brief review of PortaAPL for the Mac. I'll spend as few articles as possible in straight APL tutorial mode before starting in on Mac-specific features and bigger programs.

APL, A Programming Language, was developed by Kenneth Iverson at IBM in the early 60's, which makes it one of the older computer languages. It is more of a mathematical notation that can be interpreted by a computer than a traditional computer language, and handles data in groups (arrays) rather than just in small pieces.

Though it's a natural language for scientific and mathematical problems, in the U.S. it seems to be used mostly as a business language. Almost all of the textbooks currently in print are slanted towards business. Insurance companies in particular like it, because APL is ideal for statistical programs. In Europe and Canada it caught on more quickly and is used a lot in the scientific community for data analysis.

Since it is an interpreter, APL runs slower than most compilers. But it runs much faster than other interpreters such as Basic. The programs are much shorter, so APL spends most of its time working on the problem, not interpreting the commands. You also get the advantages of an interpreter - interactive writing and debugging. Functions are defined by name as in Forth or Logo, and you can define local variables, so it's easy to define new functions as you go along.

APL is extremely powerful. It's recursive and self-modifying, with dynamic allocation of data storage. It does calculations on entire arrays at once, not just element by element. This power has it's disadvantages, the primary one being memory limitations. Arrays require lots of memory. On a 512k Mac, the largest two dimensional array of integers you can deal with is about 225 by 225, which isn't big enough for many problems. Sometimes you have to use a less elegant algorithm to solve a problem. But then, who has ever had enough memory?

The peculiar character set APL uses (þ,©,“,ð,‰,Â, etc.) has given it the reputation of being unreadable. Pascal programmers have been heard to say that APL programmers hand each other sections of code and say 'Bet you can't figure out what this does!'. But APL isn't really harder to read than other languages once you know what the symbols mean. And since APL can work on groups of data all at once, instead of just a little at time, the code is much more compact. You can see the whole thing at once, instead of getting lost manipulating indices in the middle of triply nested loops.

APL is very different from Fortran, C, Forth, or Lisp. The symbols, syntax, and natural algorithms all take some getting used to. The first hurdle is the keyboard layout. Unshifted characters give upper-case letters, shifted characters and numbers give the APL symbols. You'll need to keep the keyboard map handy for reference at first, though most of the symbols are easy to find once you know the mnemonics (i.e. È (iota) is shift-I, ® (rho) is shift-R). If you can't stand it, there is an ASCII mode that substitutes keywords and ASCII punctuation for the symbols, but if you plan to read standard APL books, or trade programs with other APL users, it's better to do it right. The real notation is also more compact and easily read - the symbols stand out.

The next difficulty is the syntax. There is no hierarchy of operators - all APL expressions are evaluated from right to left. If you need to change the order of operation, you must use parentheses. For example, the expression 5-6-2 is equivalent to 5-(6-2) = 1 in APL, not (5-6)-2 = -3 as in most languages. And 5x6-2 equals 20, not 28.

The terminology is a bit different too. What is usually called an operator, such as + or -, is a function in APL, because it returns a value. An operator is a special thing that systematically modifies the effect of a function. Functions operate on data, and operators operate on functions. Operators are one of the things that makes APL so powerful - many mathematical ideas such as the inner product (given two vectors a,b,c and x,y,z the standard inner product is ax+by+cz, a scalar) can be generalized to any functions, not just the sum of the products.

Functions can be niladic, monadic, or dyadic. Niladic functions take no arguments - some system commands such as )OFF are niladic. Monadic functions take one argument, and are called by the sequence FUNCTION ARGUMENT, such as the factorial of N (!N). Dyadic functions, which take two arguments, are called as ARGUMENT FUNCTION ARGUMENT, as in 5 + 3. This is true of user defined functions as well as the built in primitives.

People who insist on strongly typed languages can stop here and read the Pascal article instead. The standard data types are boolean, character, integer, and floating point, but these distinctions are less important than the structure of the data into scalars, vectors, and larger arrays. An argument to any function can be a scalar, vector, or array, with a few exceptions. It is irrelevant (again with some exceptions) whether the data is a character string, an integer, or a set of boolean values. A scalar is a single number or character, without dimension or length. A vector is a set of data that forms a one dimensional array, a matrix has two dimensions. Arrays can have up to 63 dimensions in PortaAPL, though you'll never use that many - if you had two bits per dimension, you'd need a billion Gigabytes to hold the array. All elements of an array must have the same data type, so if you put a single floating point number in a large array, every element will be floating point. Type conversions are taken care of automatically and transparently at the interpreter's discretion, though there are ways to force a conversion to a particular type. On the Mac, boolean values are stored as bits, characters as bytes, integers as 4 byte words, and floating point as 8 byte long words. A character string is a vector of characters; a character matrix is a set of strings, one per row.

In addition to dividing functions into classes according to how many arguments they take, they can be divided into three groups that depend on what their effect is. Scalar functions change the data in an array, but not the shape or size of the array. Restructuring functions change the shape or dimension of an array, but not the data values. Mixed functions change both the data and the shape, and will have to wait for a later article. A scalar function can operate on arrays, not just scalars, but they operate element by element. For example, you can add two vectors of the same length:

 2 4 6 11 8  +  7 43 1 6 31

9 47 7 17 39

Iota (È) and rho (®) are two of the most important reshaping functions. Iota in it's monadic form creates a vector of integers in ascending order from a scalar: È6 gives the vector 1 2 3 4 5 6. Monadic rho gives the length of each dimension of an array: ® 1 3 5 7 is 4. Dyadic rho creates an array with the dimensions given by the left argument, filled with the values given in the right argument. The right argument is repeated as many times as necessary to fill the array:

 2 4 ® 1 2 3

1 2 3 1
2 3 1 2

Some of the scalar functions will look deceptively familiar to programmers. + and - do what you expect, but * means the exponential or power, not multiply, and / is not divide, but something more complicated - it can be either a restructuring function (compress) or an operator (reduction). When used as the reduction operator, f/ inserts the function f between each element of a vector:

 +/ 1 2 3 4

 ª/ 1 0 0 1

 -/ 1 2 3 4

Fig. 1 APL Table of Functions

There is a floating point benchmark that's become popular on usenet: how long it takes to calculate the harmonic series to 10000. This is the sum of 1/i from i=1 to 10000. Most languages take 5 or so lines to write this program; APL takes 9 characters, including the 10000. No looping is needed. Start by noticing that you want to do something with an index that ranges from 1 to 10000. Well, there's a function iota that will generate a vector with all those values. Then, you need to take the reciprocal of each value, so just use the monadic ±. To take the sum of the vector, use the reduction operator with addition. So the whole thing turns into -/±È10000. How long does it take? 49 seconds, compared to 38 seconds for Aztec C, and 69 seconds for Megamax C. APL won't always do this well compared to compilers, this set of functions is extremely efficient in APL. Notice also the amount of memory used for a vector of 10000 4-byte integers.

To turn this into a function so you can run it for any number of terms, enter the editor by typing © Z “ HARMONIC N <ret>. This line will appear as line zero at the top of the screen. Any variable appearing on line zero is a local variable, all others are global. If you don't assign the function to a dummy variable (Z in this case), you won't be able to call it from another function later. Next, starting with line one, type in your program. At some point, the final value of the program must be assigned to the same dummy variable as in line zero. When you're finished, the screen should look like this:

         [1]    Z “ -/±ÈN

You can move the cursor with the mouse. The lamp symbol (Ê) can be used at the beginning of a line to put in comments. Use command-z or the EDIT menu to exit the editor. To define a dyadic function, put one argument in front of the function name, and one after. DICE will roll N dice with M sides each:

©Z “ N DICE M; A; B 
 [1]    Ê  D&D dice roller - 
 rolls N M-sided dice
 [2]    A “ N ® M
 [3]    B “ œA
 [4]    Z “ -/B
 [5]    Ê This can also be written on 
 one line as:
 [6]    Ê  Z “ -/œN®M  or  Z “ -/B“œA“N®M

Line 2 sets up a vector with the same number of elements as there are dice and with each element equal to the numbers of sides of the die. The scalar operator ?R chooses a random element of the vector (ÈR), and if you give ? a vector as an argument it will find the random number for each element. Line 3 just adds up all of the independent rolls. What sort of changes need to be made to roll several dice with different numbers of sides? None! M can be a vector containing the sides of each die. Make sure the number of elements agrees with the value of N, however, or you will get the wrong result. There's at least one way to take care of this without changing the function, and I'll leave the finding of it as a puzzle.

In addition to the built in functions, APL has a number of system functions and system commands that are fairly standard from one implementation to another. System commands are prefaced with a right parenthesis, such as )OFF to exit to the finder, )SAVE FILENAME to save the workspace, and )COPY FILENAME to add a workspace to the current workspace. System functions, or quad functions, start with the quad symbol ¬. These can be used to inquire about or set various system variables, such as the precision of results with ¬PP, or to manipulate files. There are also quad versions of ¬OFF, ¬COPY and some other commands so that they can be called from inside of functions.

PortaAPL is written and distributed by Portable Software in Cambridge MA (60 Aberdeen Avenue, Cambridge, MA 02138; (617) 547-2918). A 512k Mac is required. The latest version is 2.0m. The price is $275 for the interpreter, some very clear documentation, and several workspaces with sample programs and functions to access parts of the toolbox. Upgrades are available for $25. Best of all, the interpreter isn't copy protected, so you can run it easily from a hard disk.

Portable Software has been writing APL interpreters for several years and for several different computers including Vax and the IBM PC. PortaAPL is a full IBM mainframe implementation, with lots of nice extensions. In fact, the main reference manual provided is the official IBM APL manual. The differences to the IBM version are an improved screen oriented editor, and the workspace id format which is system dependent. (Workspace id is a fancy term for program file name.) Extensions include ASCII mode, access to the Mac file system (though not the resource fork), and a way to write assembly language functions. The keyboard is a standard APL keyboard, with extensions for the Mac. Overstruck characters can be typed with the option key instead of key1-backspace-key2.

Another nice feature is a built in Datamedia 1520 APL terminal emulator. If you have access to APL on another computer, you can call up and work from your Mac. The Mac allows an improved keyboard set up, so you can send lowercase characters if you need to by holding down the option key. Unfortunately, there is no file transfer, so you can't send programs back and forth with it.

The program is written in C so it isn't as compact or as fast as it could be. It's 180k, and allows a work area of about 221k. Integers are 4 bytes and floating points numbers are 8 bytes with a range of +/- 1.79E308. Floating point calculations use the SANE package. The largest allowed function is 9999 lines; the maximum length of a symbol is 77 characters and underscores are allowed in symbol names. 16 files can be opened at once.

Editing functions and character matrices is acceptable, and certainly an improvement over the standard APL editor. (Try reading through some of the descriptions of editing a function in one of the books listed at the end!) The cursor is moved with the mouse, and there are menu and keyboard commands to insert and delete lines. Because of the nice fonts on the Mac, overstruck characters are typed in with the option key, so the editor can be in insert mode all of the time. Cut and paste aren't available, which can be a nuisance.

An excellent DRILL program is provided, that randomly generates APL expressions at 5 levels for you to evaluate. Since it evaluates the answer you give it and compares that to the evaluation of the problem, you can add to the challenge by coming up with expressions that are equivalent, but use different functions.

One of the workspaces that comes with the package is called Goodies. It has lots of useful stuff in it, including peek and poke, a sound function for playing simple tunes, mouse functions, printer i/o redirection, and functions to let you access the modem port. There are also routines that copy text or graphics from the clipboard to the screen, or text from the screen to the clipboard.

As of 2.0m, PortaAPL supports much of quickdraw and menus, but not windows, resources, or controls. Desk accessories are available from inside the interpreter. Although the EDIT menu doesn't show the standard cut, copy, and paste functions, command-X, and V are functional when desk accessories are active. The calls to quickdraw and the menu manager are straight-forward. To use them, you need to include the appropriate workspace. The quickdraw workspace includes functions for line drawing, rectangles, ovals, arcs, text fonts and scroll regions. Not all of quickdraw is supported - getpixel, regions, pictures, copybits, and icons are all missing. The lack of getpixel is going to make it much harder for me to do screen dumps to my non-Imagewriter printer. Menus are easily created by assigning a character matrix containing the items and the functions they run to a menu id number, then installing the menu. The interrupts are handled by APL if you are at the command level. From inside of programs, there is a function ¬GETKEY that lets you ask for the appropriate events. Windows aren't supported, but you can define regions for input and output.

All in all, PortaAPL is a nice job. I haven't found any bugs yet while putting in sample programs from various sources. There is one thing to beware of - you must remember to save your work before you exit, because APL won't remind you. It would be nice to have complete access to the toolbox, but that will come eventually - if not from Portable Software, then from anyone who is willing to do a bit of assembly language programming. It's expensive compared to Basic, but it's much more fun.

I hope this has been enough to get you started. I recommend that you buy or borrow an APL text book and work through plenty of examples, as well as the Drill program. Next month, I'll get into loops and branches, the other operators, and a few ways to generate prime numbers.

A Programming Language by Kenneth E. Iverson

John Wiley & Sons 1962

An Introduction to APL by S. Pommier

Cambridge University Press 1984

APL: An Interactive Approach by Gilman & Allen J. Rose John Wiley & Sons 1983

Structured Programming in APL by D. Geller & D. Freedman

Winthrop 1976


Community Search:
MacTech Search:

Software Updates via MacUpdate

Dash 3.0.2 - Instant search and offline...
Dash is an API Documentation Browser and Code Snippet Manager. Dash helps you store snippets of code, as well as instantly search and browse documentation for almost any API you might use (for a full... Read more
FontExplorer X Pro 5.0 - Font management...
FontExplorer X Pro is optimized for professional use; it's the solution that gives you the power you need to manage all your fonts. Now you can more easily manage, activate and organize your... Read more
Typinator 6.6 - Speedy and reliable text...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more
Arq 4.12.1 - Online backup to Google Dri...
Arq is super-easy online backup for the Mac. Back up to your own Google Drive storage (15GB free storage), your own Amazon Glacier ($.01/GB per month storage) or S3, or any SFTP server. Arq backs up... Read more
Gutenprint 5.2.11-pre1 - Quality drivers...
Gutenprint is a suite of printer drivers that may be used with most common UNIX print spooling systems, including CUPS, lpr, LPRng, or others. Gutenprint currently supports over 2000 printer models.... Read more
Dropbox 3.6.8 - Cloud backup and synchro...
Dropbox is an application that creates a special Finder folder that automatically syncs online and between your computers. It allows you to both backup files and keep them up-to-date between systems... Read more
Evernote 6.0.15 - Create searchable note...
Evernote allows you to easily capture information in any environment using whatever device or platform you find most convenient, and makes this information accessible and searchable at anytime, from... Read more
MacUpdate Desktop 6.0.7 - Search and ins...
MacUpdate Desktop 6 brings seamless 1-click installs and version updates to your Mac. With a free MacUpdate account and MacUpdate Desktop 6, Mac users can now install almost any Mac app on macupdate.... Read more
Freeway Pro 7.0.4 - Drag-and-drop Web de...
Freeway Pro lets you design and build sophisticated responsive websites code free. With its user-oriented drag-and-drop interface, Freeway Pro helps you piece together the website of your dreams. Add... Read more
VueScan 9.5.18 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more

Vector 2 is Officially a Thing and it...
Vector is a pretty cool parkour-driven runner that's gotten a pretty decent following since it first came out - although personally I think more people could stand to show it some love. Anyway, Nekki has announced that a sequel isofficially on its... | Read more »
This Week at 148Apps:June 22-26, 2015
June's Summer Journey Continues With 148Apps How do you know what apps are worth your time and money? Just look to the review team at 148Apps. We sort through the chaos and find the apps you're looking for. The ones we love become Editor’s Choice,... | Read more »
LEGO® Minifigures Online (Games)
LEGO® Minifigures Online 1.0.1 Device: iOS iPhone Category: Games Price: $4.99, Version: 1.0.1 (iTunes) Description: | Read more »
World of Tanks Blitz celebrates its firs...
Today marks the first anniversary of the launch of World of Tanks Blitz, the mobile version of the PC tank battler, World of Tanks. World of Tanks Blitz launched on iOS and Android on June 26th last year and to celebrate, Wargaming is giving all... | Read more »
Heroes and Castles 2 Has its Own Standal...
Heroes and Castles 2 is a third-person castle defense game from the same team behind Block Fortress and Bug Heroes. It's cool, it's fun, and now it has its very own free version. [Read more] | Read more »
Formula Cartoon All-Stars Lets You Race...
Ever want to pit your favorite characters from shows like Steven Universe, Adventure Time, and Regular Show against each other in a not quite death race? Well once upon a time you could, but Formula All Stars Touch N' Go doesn't exist anymore. Hope... | Read more »
Retype - Typography Photo Editor (Photo...
Retype - Typography Photo Editor 1.0 Device: iOS Universal Category: Photography Price: $2.99, Version: 1.0 (iTunes) Description: Retype is built out of passion for great typography and it's all about adding text to photo with style... | Read more »
Hungry Shark Evolution Celebrates Shark...
Shark Week is almost here, as is Independence Day, so naturally Hungry Shark Evolution is going to get in on the action. Yes, even the fireworks. [Read more] | Read more »
The New Trivia Crack Will Feature a Musi...
It's official: iHeartMedia (you may know them from iHeartRadio) will be in charge of providing music-related questions for Trivia Crack's upcoming sequel. Also Trivia Crack is getting a sequel. [Read more] | Read more »
Toca Life: City (Education)
Toca Life: City 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: Welcome to Toca Life: City, a metropolis filled with everyday fun! Customize characters, explore exciting locations and... | Read more »

Price Scanner via

OtterBox Releases New Symmetry Series Metalli...
Otterbox’s new Symmetry Series of smartphone cases flaunts the best of both both street style and street smarts with their new metallic finishes and trusted OtterBox protection for iPhone 6 and... Read more
MacBook Airs on sale for up to $75 off MSRP
Save up to $75 on the purchase of a new 2015 13″ or 11″ 1.6GHz MacBook Air at the following resellers. Shipping is free with each model: 11" 128GB MSRP $899 11" 256GB... Read more
Apple’s Education discount saves up to $300 o...
Purchase a new Mac or iPad at The Apple Store for Education and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free,... Read more
Save up to $600 with Apple refurbished Mac Pr...
The Apple Store has Apple Certified Refurbished Mac Pros available for up to $600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The... Read more
Mac Pros on sale for up to $260 off MSRP
B&H Photo has Mac Pros on sale for up to $260 off MSRP. Shipping is free, and B&H charges sales tax in NY only: - 3.7GHz 4-core Mac Pro: $2799, $200 off MSRP - 3.5GHz 6-core Mac Pro: $3719.99... Read more
Save up to $400 on 2014 15-inch Retina MacBoo...
B&H Photo has previous-generation 2014 15″ Retina MacBook Pros on sale for up to $400 off original MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.2GHz Retina MacBook Pro... Read more
15-inch Retina MacBook Pros on sale for up to...
B&H Photo has new 2015 15″ Retina MacBook Pros on sale for up to $125 off MSRP including free shipping plus NY sales tax only: - 15″ 2.2GHz Retina MacBook Pro: $1899.99 $100 off - 15″ 2.5GHz... Read more
College Student Deals: Additional $100 off Ma...
Take an additional $100 off all MacBooks and iMacs at Best Buy Online with their College Students Deals Savings, valid through July 11, 2015. Anyone with a valid .EDU email address can take advantage... Read more
Apple refurbished Time Capsules available for...
The Apple Store has certified refurbished Time Capsules available for $100 off MSRP. Apple’s one-year warranty is included with each Time Capsule, and shipping is free: - 2TB Time Capsule: $199, $100... Read more
Newsweek Launches iPhone App
The venerable weekly news magazine Newsweek, owned by IBT Media, has announced the launch of its first iPhone app. The new app is available through Apple’s App Store and will allow consumers to read... Read more

Jobs Board

Architect / Senior Software Engineer, *Apple...
**Job Summary** Apple Pay is already changing our pay-habit in a deeper and fundamental level. We are looking for a software engineer with a passion for large scale inte Read more
Project Manager, *Apple* Retail New Store O...
**Job Summary** An Apple Retail New Store Openings & Remodels Project Manager is responsible for successfully managing the openings, remodels, and small works of Read more
*Apple* Solutions Consultant - Retail Sales...
**Job Summary** As an Apple Solutions Consultant (ASC) you are the link between our customers and our products. Your role is to drive the Apple business in a retail Read more
Program Manger, WW *Apple* Direct Fulfillme...
**Job Summary** We are seeking a business analyst to work within our Worldwide Apple Direct Fulfillment Operations team. This role will work closely with related program Read more
Hardware Design Validation Engineer - *Apple...
**Job Summary** The Apple Watch team is looking for a Hardware Design Validation Engineer. This person will be part of the Apple Watch hardware team with Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.