Learn APL on the "00245 Raspberry Pi
Total Page:16
File Type:pdf, Size:1020Kb
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 Alan Perlis, one of the giants of Computer Science. 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] ⍝ display histogram of positive integer values hist 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<1⌽s←1,⍵∊⍺} wc 'How many beans make five?' 5 ' :"' wc 'He responded:"Can I quote you on that?"' 8 Balanced Parentheses (Chapter 2) bp returns 0 if parentheses are unbalanced, 1 if they are balanced bp←{0=+/1 ¯1 0['()'⍳⍵]} bp '((wow))' 1 bp '' 1 bp '(out of balance))' 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.