Learn APL on the $5 Raspberry Pi Your fast track from ideas to code

Romilly Cocking

This book is for sale at http://leanpub.com/learnapl

This version was published on 2016-08-18

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License Tweet This Book!

Please help Romilly Cocking by spreading the word about this book on Twitter! The suggested hashtag for this book is #learnaplonthe$5pi. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#learnaplonthe$5pi This book is dedicated to Kenneth Iverson, who gave us APL, and Eben Upton, who gave us the Raspberry Pi. Contents

Learn APL - book extract ...... 1

Introduction ...... 2 Who should read this book ...... 2 Early access ...... 2 Online support ...... 2 What you will learn...... 2 Reasons to learn APL ...... 3 Myths about APL ...... 3 Reasons not to learn APL ...... 4 APL power examples ...... 5 The Try APL website ...... 6 Running APL on your own hardware ...... 6 Conventions in this book ...... 6

Chapter 1 ...... 7 Getting started ...... 7 Array programming without explicit loops ...... 8 Assigning values to variables ...... 9 A shortcut to counting ...... 10 Illuminate your code - use comments ...... 11 Catenate ...... 12 System commands ...... 12 Finishing your session ...... 14 Exercises ...... 15 The Dyalog APL keyboard ...... 15

Appendix 1 ...... 17 Installing APL on the Raspberry Pi ...... 17 Learn APL - book extract

This is an extract from a new Introductory book on APL. It features Dyalog’s free implementation on the Raspberry Pi, but you can also use it if you are learning Dyalog APL on Windows, OS/X or other Linux systems. I hope it will motivate you to take a look at this powerful language and help you to get started. The book is ideal if you are entering the Dyalog annual problem-solving competition. An early access version of the book is available on Leanpub. All Leanpub purchases offer an unconditional 45-day money-back guarantee, and purchasers get future updates for free. Introduction

Who should read this book

You should read this book if you want to

• find out what programming in APL is like • learn how to use the language effectively • decide if APL is appropriate for your project • take part in the Dyalog annual APL problem-solving competition

The book is written for people learning Dyalog APL. You can run Dyalog APL on a Raspberry Pi for free. There are also free educational and personal licenses available for Windows, other Linux computers and OS/X. You’ll find more information on the Dyalg APL website1

Early access

The book is currently incomplete and is offered in Early Access format at a reduced price. Once you have purchased the eBook you will be entitled to future updates free of charge, and you are also covered by Leanpub’s 45 day unconditional money-back guarantee.

Online support

The book now has a Google Group2 to support reviewers and readers.

What you will learn.

You’ll learn enough APL to write useful code and you will be able to continue your exploration of APL using other resources. This book does not cover advanced topics like web applications, GUIs or interfaces with the Operating System. It does suggest resources which cover those topics. One great fact about APL is that you can do a lot with a little knowledge. In the section on APL power examples later in this introduction you can see some of the code you’ll have mastered in the first three chapters of this book.

1http://www.dyalog.com/ 2https://groups.google.com/forum/#!forum/learn-apl Introduction 3

Reasons to learn APL

Here are some good reasons to learn this powerful language

1. APL is concise and expressive, so you can try out new ideas very quickly. 2. APL is focused on arrays, so it will change the way you think about programming (in a good way). 3. APL is challenging and fun, so it will help you stretch your mind. 4. The APL community is full of bright and helpful people, so you will expand your network of contacts. 5. The demand for APL developers exceeds the supply, so knowing APL can help you find a job. Myths about APL

Here’s a story from APL’s distant past. It was originally told by Professor , one of the giants of .

Ken (Iverson) was showing some slides — and one of his slides had something on it that I was later to learn was an APL one-liner. And he tossed this off as an example of the expressiveness of the APL notation. I believe the one-liner was one of the standard ones for indicating the nesting level of the parentheses in an algebraic expression. But the one- liner was very short — ten characters, something like that — and having been involved with programming things like that for a long time and realizing that it took a reasonable amount of code to do, I looked at it and said, “My God, there must be something in this language.” Bauer, on my left, didn’t see that. What he saw or heard was Ken’s remark that APL is an extremely appropriate language for teaching algebra, and he muttered under his breath to me, in words I will never forget, “As long as I am alive, APL will never be used in Munich.” And Dijkstra, who was sitting on my other side, leaned toward Bauer and said, “Nor in Holland.” The three of us were listening to the same lecture, but we obviously heard different things.

Taken from a transcription of a talk given by Professor Perlis at the APL’78 Conference held at Foothill College, Los Altos, CA. on 1978-03-29. via Roger Hui. You can read the full transcript here3 One of the many things that distinguished Perlis was his growth mindset4. Some people don’t have that growth mindset. They look at what’s different about APL, or recall myths that they have heard, and decide to ignore the language without investigating it. Here are a few of the myths I still hear about APL, along with my perspective on them.

3http://www.jsoftware.com/papers/perlis78.htm 4http://mindsetonline.com/whatisit/about/ Introduction 4

Myth: APL is hard to read.

APL is hard to read if you haven’t learned the language. So is Chinese, but there are over a billion people who can read Chinese, and APL is much, much easier to learn! It takes most people a few days to ‘get their eye in’ to the point where the APL characters no longer look strange. From then on they can read well-written APL with ease, and they can read bad APL code if they have to.

Myth: APL is hard to learn

It is, for some people, but not for most. I’ve taught hundreds of students, and talked to instructors who have taught thousands more. We’ve had broadly similar experiences. Of our large group of students

• some became expert. They fell in love with the language and used it whenever they could. • most became competent. They used APL effectively when they needed to. • a few disliked it. • one or two became outstanding. They produced a steady stream of elegant, expressive, innovative solutions over many years.

Many of the past winners of Dyalog’s annual problem solving contest learned enough APL to win the competition in one or two weeks.

Myth: APL is slow

Badly written APL is about as slow as badly written Python. Well-written APL is as fast as (or faster than) well-written C.

Myth: APL is dead

It’s true that the hundreds of thousands of 1980s customers who used IBM mainframe APL packages like ADI and ADRS have long since migrated to using databases, report generators and spreadsheets. By contrast, the number of serious APL developers has grown steadily, and today APL skills are in demand. Most of the large APL users run apprenticeship schemes or their equivalent to ensure a steady supply of skilled APL developers.

Reasons not to learn APL

This section intentionally left blank Introduction 5

APL power examples

Here are some concise APL expressions. Each does something useful, and you will learn how and why to read and write code like these examples early in the book. The titles below tell you which chapter each example comes from. TODO: Add examples to chapters and review content plan.

Histogram (Chapter 2)

hist←'.⎕'[x∘.>⍳⌈/x]⍝displayhistogramofpositiveintegervalues hist3105 2 ⎕⎕⎕...... ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕ ⎕⎕⎕⎕⎕..... ⎕⎕......

Word Count (Chapter 2) wc is like the Unix word count. wc ignores leading, trailing and repeated delimiters. The default delimiter for wc is space.

wc← {⍺←''⋄+/s<1⌽s←1,⍵∊⍺} wc'Howmanybeansmakefive?' 5 ' :"'wc'Heresponded:"CanIquoteyouonthat?"' 8

Balanced Parentheses (Chapter 2) bp returns 0 if parentheses are unbalanced, 1 if they are balanced

bp←{0=+/1¯10['()'⍳⍵]} bp'((wow))' 1 bp'' 1 bp'(outofbalance))' 0

Prime numbers (Chapter 3)

Find all the primes up to N using the Sieve of Eratosthenes Introduction 6

primes←{(2=+⌿0=c∘.|c)/c←⍳⍵} primes 10 2 3 5 7 primes 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Linear Regression ()Chapter xx)

TBD

The Try APL website

TBD

Running APL on your own hardware

See the appendices.

Installing APL on your Raspberry Pi

Assuming that you have a Raspberry Pi set up and connected to the internet, you can download and install APL in a matter of minutes. Details are in Appendix 1.

Conventions in this book

TBD Chapter 1

Getting started

Time to start APL - and start learning! To begin your first APL session on a Raspberry Pi, run Dyalog from the Programming menu. (If you’re using RIDE to connect to a Raspberry Pi or some other computer with APL installed, follow the instructions in Appendix 2 once I’ve written them!)

Dyalog menu

The APL environment includes a REPL5 - a Read-Evaluate-Print-Loop. That means that you can write code and try it out right away. That’s a great way to learn a language, and it’s also a great way to develop software. In the examples that follow, the code that you type is indented by six spaces. Once you start your session you’ll see that APL inserts those spaces for you when it’s your turn to type. APL’s output is not indented, so you can see what you should type and what you should expect APL to output. So - here goes. Type

2 + 2 4

Try some more:

5https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop Chapter 1 8

10 - 4 6 4 - 5 ¯1

Note how APL represents negative numbers using a ¯ symbol. The ¯ (called high minus) is part of the way you write the value negative one. That’s different from the - symbol (called minus) which tells APL to do a subtraction. Plus and minus are primitive functions in APL. What about multiply and divide? Of course APL does those too. APL uses the same symbols that I was taught at school: × for multiply, and ÷ for divide. If you’re using a Windows keyboard you can hold down the Windows key to type APL symbols. You may need to configure your keyboard for APL. You’ll find instructions in Appendix 3.

• Type Windows- and you should see a × symbol • Type Windows-equals to get the ÷ sign.

You can find more about how to type APL symbols at the end of this chapter. Time to try out multiplication and division.

2 × 3 12 5÷2 2.5 12÷4 3 4÷3 1.333333333

Now for something rather different. Try the experiment below,

1 2 3 + 4 5 6 5 7 9

What’s going on? Array programming without explicit loops

APL treat the two lists of numbers as vectors and it adds the corresponding elements together. A lot of calculations need to be done on vectors, and APL’s built-in looping makes this really easy. Try some more examples: Chapter 1 9

2 3 4 5 × 1 2 1 2 2 6 4 10 3 4 2 5 - 4 0 ¯1 3 ¯1 4 3 2 120 ÷ 2 3 4 5 6 60 40 30 24 20 0.1 0.1 0.1 0.1 × 3 5 4 2 0.3 0.5 0.4 0.2

That last example works, but it’s a bit tedious to type. Fortunately there is an easy shortcut.

0.1 + 3 5 4 2 3.1 5.1 4.1 2.1

If you ask APL to multiply (or add, or subtract, or divide) a number on its own and a vector of numbers, APL will use the single number repeatedly. A single number on its own is called a scalar. The repeated use of a scalar when you’re adding it to a vector is called scalar extension. What happens if you try to add two vectors of different lengths?

1 2 3 + 4 5 6 7 LENGTH ERROR 1 2 3+4 5 6 7 ∧

APL doesn’t kow what you want to do, so it treats the expression as an error.

Don’t worry about causing APL errors. APL will try to tell you what went wrong, and you won’t break anything :)

Assigning values to variables

It would be rather tedious if you had to type values in to APL every time you wanted to use them. Fortunately, you can tell APl to remember values you want to use repeatedly. Suppose you are currently 23. Type: Chapter 1 10

age ← 23 age + 10 33

The first line you typed told APL to assign the value 23 to a new variable age. In the second line you asked APL to add 10 to your current age, and APL displayed the result. Notice that APL will display a result if you don’t tell it what to do with it. APL variables can contain vectors as well as scalars.

ages ← 12 23 19 ages 12 23 19 ages×2 24 46 38

A shortcut to counting

In one of the earlier examples you added the vector 1 2 3 to the vector 4 5 6.

Mathematicians call vectors like that arithmetic progressions, and you may well need to use them in your software.

APL has a particularly easy way to create arithmetic progressions using the ⍳ function. Here are some examples:

⍳3 1 2 3 3 + ⍳3 4 5 6 (⍳3) + 3 + ⍳3 5 7 9 2 × ⍳5 2 4 6 8 10

By default APL starts counting at one. In Chapter 6 you will see a way to get APL to start counting at zero. Some programs are simpler when written that way. Chapter 1 11

Illuminate your code - use comments

As you get more experienced in APL programming the code you write will get more complex. Most code is read more often than it is written, so you should consider documenting it using comments.

The APL symbol for a comment is ⍝ - often called lamp because it’s intended to illuminate your code.

Whenever the APl interpreter encounters a comment it ignores the rest of that line. You’ll find two styles of comment widely used in APL code.

1. A stand-alone comment starts with a lamp symbol. That means that nothing on that line will get executed. 2. An in-line comment follows some executable code on the same line.

The comments should explain what the code does or why it is written that way.

When should you comment? The Three AM rule

I first heard this tip at a conference many yeas ago. It’s called The three AM rule, and it applies to programming in any language. Here’s how I once heard the presenter explain the rule:

Imagine that you’re asleep at home at 3 o’clock in the morning.

The phone rings. And rings. And rings.

You answer it.

‘Hi there. The production system has just fallen over. Can you fix it?’

When you take a look at the application, what style of code do you hope you’ll see? That’s the way you should code.

That’s the three AM rule: write code that you, or other developers, would be relieved to see if they are trying to fix a problem at three o’clock in the morning. If comments would help you or others to read your code at 3 AM, add those comments! Chapter 1 12

Catenate

So far you’ve seen ways of combining vectors based on arithmetic functions. There’s another common way to create new arrays from old: by joining them together. In APL, a , (comma) is the catenate function. Try it out:

1 2 3, 6 5 4 1 2 3 6 5 4 1, 4 7 11 1 4 7 11 5 3 7, 0 5 3 7 0 2 3, 5 6 8 2 3 5 6 8

You can catenate any two vectors, or a vector and a scalar, or a scalar with a vector. Later in the book you will see that there are even more possibilities.

System commands

If you’ve been working on an APL session for a while it can be useful to check what variables you have created. APl has a system command to do that. System commands in APL don’t create values, but they do other useful things. One such command will tell you the names of all the variables you have defined. Try it out:

)vars age ages

In APL, system commands start with an open right bracket. The vars command tells you the name of the variables that are currently defined. When you work in an APL session, the variables you create are held in what APL calls the current workspace. A workspace can also contain functions and other things. We’ll cover these later in this book. When you have finished an APL session, you can save the contents of your workspace, and return to it when next you use APL. Try the following commands: Chapter 1 13

)wsid course was CLEAR WS )save course saved Sun May 22 17:09:17 2016

What did that do? The first command gave a name to your workspace. Previously it had no name, so APL called it CLEAR WS (a clear workspace). Then you asked APL to save your workspace. It stored it on disk. If you look in your home directory, you should see a file called course.dws.

Windows may hide the dws extension.

It’s a binary file, so don’t try to edit it! Names are useful. If you are working on more than one project, you can have several workspaces, one for each project. Each has a name which will help you find the workspace you want to use for any given session. You can find out all your local workspaces using the )LIB command. Here’s what happened when I ran it:

)lib . startapl.dws /opt/mdyalog/15.0/32/unicode/ws apl2in.dws apl2pcin.dws buildse.dws conga.dws ddb.d\ ws dfns.dws display.dws eval.dws fonts.dws ftp.d\ ws groups.dws isolate.dws loaddata.dws max.dws min.dws ops.d\ ws postscri.dws quadna.dws rconnect.dws salt.dws sharpplot.dws smdemo.dws smdesign.dws smtutor.dws sqapl.dws tube.dws tutor.dws util.dws xfrcode.dws xlate.dws /opt/mdyalog/15.0/32/unicode/samples/fun intro.dws life.dws sudoku.dws

Wow! Lots of workspaces. The first two lines show that there is a workspace called startapl.dws in the current directory. Chapter 1 14

That’s a workspace that I saved earlier. It contains the functions and variables used in this course. The next line shows that there is a directory called /opt/mdyalog/15.0/32/unicode/ws which contains 31 workspaces. That directory and the workspaces in it are created by Dyalog during the installation process. There’s also a Dyalog directory called /opt/mdyalog/15.0/32/unicode/samples/fun which contains some fun workspaces including an implementation of Conway’s Game of Life6 and the game of sudoku.

Finishing your session

Once you’ve finished a session you can close APL down by typing a system command:

)off

APL will close down. If you want to get your work back, restart APL from the Programming menu. You will start a new session with a new clear workspace. To resume your work you must load your saved workspace. Type

)load course ./course saved Sun May 22 17:09:17 2016

Now you can check that the workspace still contains your variable:

)vars age ages age 23

Well done! You’ve taken the first step to mastering APL. Now try the following simple exercises to consolidate what you’ve learned.

6https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Chapter 1 15

Exercises

1.1

Create a variable called income containing the vector 10000 11570 11000 12550. (Imagine this contain someone’s income for the last four quarters of the year.) Create another variable containing the vector 7250 8345 9547 12650. This might show how much that person spent in each quarter. Now calculate and display what they saved each quarter. Of course, if they spent more than they earned the savings will be negative.

1.2

Create a variable weights containing the numbers 10.2 8,3 7.5 and convert from pounds to kilogrammes. A pound is roughly 0.45 kilogrammes.

The Dyalog APL keyboard

Here’s the layout of the UK APL keyboard. TODO: Explain about key shifts and move earlier in text. Chapter 1 16

Dyalog APL keyboard Appendix 1

Installing APL on the Raspberry Pi

You can download and install a copy of APL for the Raspberry Pi from Dyalog. It’s free for personal use. If you want to use it for work, or create a product that you or others sell, you will need to get a commercial license. You can install and run Dyalog APL on the following Raspberry Pi versions:

Pi 1 Model A Pi 1 Model A+ Pi 1 Model B Pi 1 Model B+ Pi 2 Model B Pi 3 Model B Pi zero (all versions)

The current version (15.0) of Dyalog APL will run on the wheezy and jessie versions of Raspbian. Once installed, Dyalog requires 100 MB of storage. You will need another 100 MB temporarily for the installation image. You can find installation instructions here7.

7http://packages.dyalog.com/