Top Banner
22

Learn APL on the '00245 Raspberry Pisamples.leanpub.com/learnapl-sample.pdf · Introduction 3 ReasonstolearnAPL Herearesomegoodreasonstolearnthispowerfullanguage...

Mar 18, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Learn APL on the $5 Raspberry PiYour 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 LeanPublishing process. Lean Publishing is the act of publishing an in-progress ebookusing lightweight tools and many iterations to get reader feedback, pivot until youhave the right book and build traction once you do.

    This work is licensed under a Creative CommonsAttribution-NonCommercial-ShareAlike 4.0 International License

    http://leanpub.com/learnaplhttp://leanpub.com/http://leanpub.com/manifestohttp://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_UShttp://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_UShttp://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US

  • 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 tosearch for this hashtag on Twitter:

    https://twitter.com/search?q=#learnaplonthe$5pi

    http://twitter.comhttps://twitter.com/search?q=%23learnaplonthe\protect \char "0024\relax 5pihttps://twitter.com/search?q=%23learnaplonthe\protect \char "0024\relax 5pi

  • This book is dedicated to Kenneth Iverson, who gave us APL, and Eben Upton, whogave us the Raspberry Pi.

  • Contents

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

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

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

    Appendix 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Installing APL on the Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . 17

  • Learn APL - book extractThis is an extract from a new Introductory book on APL. It features Dyalog’s freeimplementation on the Raspberry Pi, but you can also use it if you are learning DyalogAPL 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 toget 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, andpurchasers get future updates for free.

  • IntroductionWho 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 educationaland personal licenses available for Windows, other Linux computers and OS/X. You’llfind more information on the Dyalg APL website1

    Early access

    The book is currently incomplete and is offered in Early Access format at a reducedprice. Once you have purchased the eBook youwill be entitled to future updates freeof charge, and you are also covered by Leanpub’s 45 day unconditional money-backguarantee.

    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 yourexploration of APL using other resources.

    This book does not cover advanced topics like web applications, GUIs or interfaceswith 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 ofthe 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

    http://www.dyalog.com/https://groups.google.com/forum/#!forum/learn-aplhttp://www.dyalog.com/https://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 yournetwork of contacts.

    5. The demand for APL developers exceeds the supply, so knowing APL can helpyou find a job.

    Myths about APL

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

    Ken (Iverson) was showing some slides — and one of his slides hadsomething on it that I was later to learn was an APL one-liner. And hetossed 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 thenesting level of the parentheses in an algebraic expression. But the one-liner was very short — ten characters, something like that — and havingbeen involved with programming things like that for a long time andrealizing that it took a reasonable amount of code to do, I looked at itand said, “My God, there must be something in this language.” Bauer, onmy left, didn’t see that. What he saw or heard was Ken’s remark that APL isan extremely appropriate language for teaching algebra, and he mutteredunder 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 myother side, leaned toward Bauer and said, “Nor in Holland.” The three ofus were listening to the same lecture, but we obviously heard differentthings.

    Taken from a transcription of a talk given by Professor Perlis at the APL’78 Conferenceheld at Foothill College, Los Altos, CA. on 1978-03-29. via Roger Hui. You can read thefull 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 aboutAPL, or recall myths that they have heard, and decide to ignore the language withoutinvestigating 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.htm4http://mindsetonline.com/whatisit/about/

    http://www.jsoftware.com/papers/perlis78.htmhttp://mindsetonline.com/whatisit/about/http://www.jsoftware.com/papers/perlis78.htmhttp://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 ismuch, much easier to learn!

    It takes most people a few days to ‘get their eye in’ to the point where the APLcharacters no longer look strange. From then on they can read well-written APL withease, 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 toinstructors 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 wheneverthey 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 enoughAPL 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 fastas (or faster than) well-written C.

    Myth: APL is dead

    It’s true that the hundreds of thousands of 1980s customerswho used IBMmainframeAPL packages like ADI and ADRS have long since migrated to using databases, reportgenerators and spreadsheets.

    By contrast, the number of serious APL developers has grown steadily, and today APLskills are in demand. Most of the large APL users run apprenticeship schemes or theirequivalent 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 willlearn 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] ⍝ display histogram of positive integer valueshist 3 10 5 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

  • Introduction 6

    primes←{(2=+⌿0=c∘.|c)/c←⍳⍵}primes 10

    2 3 5 7primes 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 candownload and install APL in a matter of minutes. Details are in Appendix 1.

    Conventions in this book

    TBD

  • Chapter 1Getting started

    Time to start APL - and start learning!

    To begin your first APL session on a Raspberry Pi, run Dyalog from the Programmingmenu.

    (If you’re using RIDE to connect to a Raspberry Pi or some other computer with APLinstalled, 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 tolearn 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. Onceyou start your session you’ll see that APL inserts those spaces for you when it’s yourturn to type.

    APL’s output is not indented, so you can see what you should type and what youshould expect APL to output.

    So - here goes. Type

    2 + 24

    Try some more:5https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop

    https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loophttps://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop

  • Chapter 1 8

    10 - 46

    4 - 5¯1

    Note how APL represents negative numbers using a ¯ symbol. The ¯ (called highminus) 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? Ofcourse 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 APLsymbols.You may need to configure your keyboard for APL. You’ll find instructions in Appendix3.

    • 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 × 312

    5÷22.5

    12÷43

    4÷31.333333333

    Now for something rather different. Try the experiment below,

    1 2 3 + 4 5 65 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 elementstogether.A lot of calculations need to be done on vectors, and APL’s built-in looping makes thisreally easy.Try some more examples:

  • Chapter 1 9

    2 3 4 5 × 1 2 1 22 6 4 10

    3 4 2 5 - 4 0 ¯1 3¯1 4 3 2

    120 ÷ 2 3 4 5 660 40 30 24 20

    0.1 0.1 0.1 0.1 × 3 5 4 20.3 0.5 0.4 0.2

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

    0.1 + 3 5 4 23.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 avector of numbers, APL will use the single number repeatedly. A single number on itsown is called a scalar.

    The repeated use of a scalar when you’re adding it to a vector is called scalarextension.

    What happens if you try to add two vectors of different lengths?

    1 2 3 + 4 5 6 7LENGTH 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 wentwrong, 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 touse them. Fortunately, you can tell APl to remember values youwant to use repeatedly.

    Suppose you are currently 23. Type:

  • Chapter 1 10

    age ← 23age + 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 displayedthe 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 19ages

    12 23 19ages×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 maywell need to use them in your software.

    APL has a particularly easy way to create arithmetic progressions using the ⍳ function.

    Here are some examples:

    ⍳31 2 3

    3 + ⍳34 5 6

    (⍳3) + 3 + ⍳35 7 9

    2 × ⍳52 4 6 8 10

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

  • Chapter 1 11

    Illuminate your code - use comments

    As you get more experienced in APL programming the code you write will get morecomplex.

    Most code is read more often than it is written, so you should consider documentingit using comments.

    The APL symbol for a comment is ⍝ - often called lamp because it’s intendedto 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 onthat 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, andit applies to programming in any language. Here’s how I once heard the presenterexplain 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 hopeyou’ll see? That’s the way you should code.

    That’s the three AM rule: write code that you, or other developers, would be relievedto 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 thosecomments!

  • 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 41 2 3 6 5 4

    1, 4 7 111 4 7 11

    5 3 7, 05 3 7 0

    2 3, 5 6 82 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 whatvariables 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 thevariables you have defined. Try it out:

    )varsage ages

    In APL, system commands start with an open right bracket. The vars command tellsyou 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 callsthe current workspace.

    A workspace can also contain functions and other things. We’ll cover these later inthis 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 coursewas CLEAR WS

    )savecourse 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 APLcalled it CLEAR WS (a clear workspace).

    Then you asked APL to save your workspace. It stored it on disk. If you look in yourhome 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 severalworkspaces, one for each project. Each has a name which will help you find theworkspace 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.dwssharpplot.dws smdemo.dws smdesign.dws smtutor.dwssqapl.dws tube.dws tutor.dws util.dwsxfrcode.dws xlate.dws

    /opt/mdyalog/15.0/32/unicode/samples/funintro.dws life.dws sudoku.dws

    Wow! Lots of workspaces.

    The first two lines show that there is a workspace called startapl.dws in the currentdirectory.

  • Chapter 1 14

    That’s a workspace that I saved earlier. It contains the functions and variables usedin this course.

    The next line shows that there is a directory called /opt/mdyalog/15.0/32/unicode/wswhich contains 31 workspaces. That directory and the workspaces in it are createdby Dyalog during the installation process.

    There’s also a Dyalog directory called /opt/mdyalog/15.0/32/unicode/samples/funwhich contains some fun workspaces including an implementation of Conway’s Gameof 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 youmust 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:

    )varsage ages

    age23

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

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

    https://en.wikipedia.org/wiki/Conway's_Game_of_Lifehttps://en.wikipedia.org/wiki/Conway's_Game_of_Lifehttps://en.wikipedia.org/wiki/Conway's_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 mightshow how much that person spent in each quarter.

    Now calculate and display what they saved each quarter. Of course, if they spent morethan they earned the savings will be negative.

    1.2

    Create a variable weights containing the numbers 10.2 8,3 7.5 and convert frompounds 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 1Installing APL on the Raspberry Pi

    You can download and install a copy of APL for the Raspberry Pi from Dyalog. It’s freefor personal use. If you want to use it for work, or create a product that you or otherssell, you will need to get a commercial license.

    You can install and run Dyalog APL on the following Raspberry Pi versions:

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

    The current version (15.0) of Dyalog APL will run on the wheezy and jessie versionsof Raspbian.

    Once installed, Dyalog requires 100 MB of storage. You will need another 100 MBtemporarily for the installation image.

    You can find installation instructions here7.

    7http://packages.dyalog.com/

    http://packages.dyalog.com/http://packages.dyalog.com/

    Table of ContentsLearn APL - book extractIntroductionWho should read this bookEarly accessOnline supportWhat you will learn.Reasons to learn APLMyths about APLReasons not to learn APLAPL power examplesThe Try APL websiteRunning APL on your own hardwareConventions in this book

    Chapter 1Getting startedArray programming without explicit loopsAssigning values to variablesA shortcut to countingIlluminate your code - use commentsCatenateSystem commandsFinishing your sessionExercisesThe Dyalog APL keyboard

    Appendix 1Installing APL on the Raspberry Pi